Results 1 to 6 of 6

Thread: Weird color problems related to QTextEdit and QPainter

  1. #1
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Unhappy Weird color problems related to QTextEdit and QPainter

    Hi!

    I am experiencing some really weird color changing problems during the creation of a program.

    See for your self:

    myclass.h
    ------------
    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4. #include <QWidget>
    5. #include <QPainter>
    6. #include <QPaintEvent>
    7. #include <QPixmap>
    8. #include <QGridLayout>
    9. #include <QTextEdit>
    10.  
    11. class myLabel : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. myLabel(QWidget *parent);
    17. ~myLabel();
    18.  
    19. protected:
    20. void paintEvent(QPaintEvent *);
    21. };
    22.  
    23.  
    24. class myClass : public QWidget
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. myClass(QWidget *parent = 0);
    30. ~myClass();
    31. QGridLayout *gridLayout;
    32. QTextEdit *textEdit;
    33. myLabel *textLabel;
    34.  
    35. };
    36.  
    37. #endif // MYCLASS_H
    To copy to clipboard, switch view to plain text mode 

    myclass.cpp
    --------------
    Qt Code:
    1. #include "myclass.h"
    2.  
    3. //myLabel
    4. myLabel::myLabel(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. }
    8.  
    9. myLabel::~myLabel()
    10. {
    11.  
    12. }
    13.  
    14. void myLabel::paintEvent(QPaintEvent *)
    15. {
    16. QPainter painter(this);
    17. //============== HERE IT IS ==============//
    18. //The color changes caused by the html are very weird.
    19. //Try to set the alpha channel to 254 and then 255 and see the difference
    20. painter.setPen(QColor(40, 40, 40, 180));
    21. painter.setFont(QFont("Times", 13, QFont::Bold));
    22. painter.drawText(0, 15, "This is a test");
    23. }
    24.  
    25. //myClass
    26. myClass::myClass(QWidget *parent)
    27. : QWidget(parent)
    28. {
    29. QString myText = "This is just a text";
    30. QColor myColor = QColor(200, 200, 200);
    31. QString html = QString("<font color=\"%1\">%2</font>").arg(myColor.name()).arg(myText);
    32.  
    33. textLabel = new myLabel(this);
    34. textEdit = new QTextEdit(this);
    35. textLabel->setFixedHeight(25);
    36. gridLayout = new QGridLayout(this);
    37. gridLayout->addWidget(textLabel, 0, 0);
    38. gridLayout->addWidget(textEdit, 1, 0);
    39.  
    40. //============== HERE IT IS ==============//
    41. textEdit->setHtml(html); //When the html is set, the text color of myLabel changes :(
    42. }
    43.  
    44. myClass::~myClass()
    45. {
    46.  
    47. }
    To copy to clipboard, switch view to plain text mode 


    First, try to comment out the line
    Qt Code:
    1. textEdit->setHtml(html);
    To copy to clipboard, switch view to plain text mode 
    to see what it is supposed to look like, and then uncomment it to see the weird things.

    When I use QTextEdit´s setHtml() and the html code includes color properties (like QString html in my example) the color of the textLabel (of class myLabel) also changes.

    In this example I use alpha channel 180 for the text of the textLabel, but try also to use alpha 254.
    I could, of course use 255, but I want to make the text more discret, and get the color of the window (I am actually having a pixmap as the background).


    If someone could help me solve this problem, I would be very greatful

    -----------
    Erlend Graff (newb to programming),
    Norway

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird color problems related to QTextEdit and QPainter

    I've tried your classes with Qt 4.2.3, but I haven't noticed any difference between the version with setHtml() and the one without it.

  3. The following user says thank you to jacek for this useful post:

    Erlendhg (13th March 2007)

  4. #3
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Wink Re: Weird color problems related to QTextEdit and QPainter

    I am using Qt 4.2.1. Maybe it is a bug that has been fixed.
    Added two screenshots. One where the alpha channel (line 20 myclass.cpp) is 254, and the other where it is 255. Watch the label saying "This is a test".

    Then I guess I should upgrade my Qt. Only thing, I heard about an issue about QMenu acting weird in that version. Is that something?

    Thanks very much
    Erlend Graff
    Attached Images Attached Images

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird color problems related to QTextEdit and QPainter

    Quote Originally Posted by Erlendhg View Post
    Only thing, I heard about an issue about QMenu acting weird in that version. Is that something?
    AFAIK the problem appears only on vista or at least the Open Source version works fine on windows XP.

  6. #5
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Smile Re: Weird color problems related to QTextEdit and QPainter

    Quote Originally Posted by jacek View Post
    AFAIK the problem appears only on vista or at least the Open Source version works fine on windows XP.
    Right. Then it's time for upgrade

    [EDIT]
    Or is it?
    Just saw this: http://www.qtcentre.org/forum/showthread.php?p=31333
    [/EDIT]

    And I think it must have been a bug in Qt 4.2.1, because I tried to compile it with Qt 4.1.4, and that worked just as it should.

    Thanks a lot
    Last edited by Erlendhg; 16th March 2007 at 19:17.

  7. #6
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Talking Re: Weird color problems related to QTextEdit and QPainter

    Hi!
    Just wanted to say that I finally found the solution of the problem.
    It was a bug, that only occured when Windows XP's ClearType was enabled:
    http://trolltech.com/developer/task-...ntry&id=142476
    Last edited by Erlendhg; 18th June 2007 at 22:58. Reason: spelling error

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.