PDA

View Full Version : QPalette help pls



munna
24th July 2006, 11:46
Hi,

I am setting the palette of a QLabel. But what I am seeing is that the color looks different on the winxp theme and it is different on the classic theme.

But when I draw something on the widget it is same on all the themes. Is this correct or am I missing something ?

Please Help
Thanks a lot.

wysota
24th July 2006, 11:50
It is correct. WindowsXP theme doesn't allow all combinations of colours for some widgets. If you want to know why -- ask Microsoft. If you draw yourself, you don't use windows calls so you have total control over what you draw.

munna
24th July 2006, 12:59
Thanks for the reply.

Is there a way by which I can make the background of a label have same color in both the themes ?

Thanks a lot.

wysota
24th July 2006, 13:43
You can always subclass windowsXP style and reimplement what you need. But you should be able to set the background of a label without it, I can't believe it's not possible...

munna
24th July 2006, 18:01
So there is no way by which i can simply change the background of the label?

jpn
24th July 2006, 19:07
The usual way works just fine for me on WinXP:


QPalette p = label->palette();
p.setColor(label->backgroundRole(), Qt::magenta);
label->setPalette(p);
label->setAutoFillBackground(true);

munna
24th July 2006, 19:22
Can you please try one more thing ?

Set the background of label as QColor(140,134,115) and draw (using the paintEvent) something on the parent widget with the same color.

Is it looking ok to you in both the xp theme and the classic theme?

Thanks a lot for your time.

jpn
24th July 2006, 20:01
I'm not sure if I understood it correctly.. :)


#include <QtGui>

class ColorDialog: public QDialog
{
protected:
void paintEvent(QPaintEvent* event)
{
// draw "something" on parent widget with the same color
QDialog::paintEvent(event);
QPainter p(this);
p.fillRect(rect(), QColor(140,134,115));
}
};

class ColorLabel: public QLabel
{
public:
ColorLabel(const QString& text, QWidget* parent = 0)
: QLabel(text, parent)
{
// a label with background palette..
setAlignment(Qt::AlignCenter);
QPalette p = palette();
p.setColor(backgroundRole(), QColor(140,134,115));
setPalette(p);
setAutoFillBackground(true);
// a frame just to indicate where are the boundaries of the label
setFrameStyle(QFrame::Box);
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ColorDialog* dialog = new ColorDialog;
ColorLabel* label = new ColorLabel("test");
QVBoxLayout* lay = new QVBoxLayout;
lay->addWidget(label);
dialog->setLayout(lay);
dialog->show();
return a.exec();
}

If you mean something different, please supply a minimal compilable example to test.