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>
{
Q_OBJECT
public:
~myLabel();
protected:
};
{
Q_OBJECT
public:
~myClass();
myLabel *textLabel;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
myclass.cpp
--------------
#include "myclass.h"
//myLabel
{
}
myLabel::~myLabel()
{
}
{
//============== 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.drawText(0, 15, "This is a test");
}
//myClass
{
QString myText
= "This is just a text";
QString html
= QString("<font color=\"%1\">%2</font>").
arg(myColor.
name()).
arg(myText
);
textLabel = new myLabel(this);
textLabel->setFixedHeight(25);
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()
{
}
#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()
{
}
To copy to clipboard, switch view to plain text mode
First, try to comment out the line
textEdit->setHtml(html);
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
Bookmarks