PDA

View Full Version : Weird color problems related to QTextEdit and QPainter



Erlendhg
12th March 2007, 16:30
Hi!

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

See for your self:

myclass.h
------------

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QPixmap>
#include <QGridLayout>
#include <QTextEdit>

class myLabel : public QWidget
{
Q_OBJECT

public:
myLabel(QWidget *parent);
~myLabel();

protected:
void paintEvent(QPaintEvent *);
};


class myClass : public QWidget
{
Q_OBJECT

public:
myClass(QWidget *parent = 0);
~myClass();
QGridLayout *gridLayout;
QTextEdit *textEdit;
myLabel *textLabel;

};

#endif // MYCLASS_H

myclass.cpp
--------------

#include "myclass.h"

//myLabel
myLabel::myLabel(QWidget *parent)
: QWidget(parent)
{
}

myLabel::~myLabel()
{

}

void myLabel::paintEvent(QPaintEvent *)
{
QPainter painter(this);
//============== HERE IT IS ==============//
//The color changes caused by the html are very weird.
//Try to set the alpha channel to 254 and then 255 and see the difference
painter.setPen(QColor(40, 40, 40, 180));
painter.setFont(QFont("Times", 13, QFont::Bold));
painter.drawText(0, 15, "This is a test");
}

//myClass
myClass::myClass(QWidget *parent)
: QWidget(parent)
{
QString myText = "This is just a text";
QColor myColor = QColor(200, 200, 200);
QString html = QString("<font color=\"%1\">%2</font>").arg(myColor.name()).arg(myText);

textLabel = new myLabel(this);
textEdit = new QTextEdit(this);
textLabel->setFixedHeight(25);
gridLayout = new QGridLayout(this);
gridLayout->addWidget(textLabel, 0, 0);
gridLayout->addWidget(textEdit, 1, 0);

//============== HERE IT IS ==============//
textEdit->setHtml(html); //When the html is set, the text color of myLabel changes :(
}

myClass::~myClass()
{

}


First, try to comment out the line
textEdit->setHtml(html); 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

jacek
12th March 2007, 23:52
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.

Erlendhg
13th March 2007, 20:31
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

jacek
14th March 2007, 20:35
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.

Erlendhg
16th March 2007, 17:50
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 :)


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


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 :)

Erlendhg
18th June 2007, 21:57
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-tracker/index_html?method=entry&id=142476