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