Yup,
You have to use QPalette now
Something like this :
Qt Code:
your_widget->setPalette(palette);To copy to clipboard, switch view to plain text mode
Guilugi.
Yup,
You have to use QPalette now
Something like this :
Qt Code:
your_widget->setPalette(palette);To copy to clipboard, switch view to plain text mode
Guilugi.
Oops
Too fast for me, Jacek.
I bow![]()
Even there is a simpler method than that:
QColor c1="<your_color>";
setPalette(c1);
jnana (15th March 2006)
Thanks all of u.
Hi All,
How to set background color of QLabel objects. I tried with setPalette(), it does not work for labels, it works for other case.
Thanks
Jnana
Hmm, it certainly does work for a QLabel..
Are you sure that you are setting the palette's color for correct role?
Btw, I suggest using the method introduced by jacek.. (just change Qt::Window -> QPalette::Window).
(Are you sure that you don't override the color in the paint event?)
Hi ,
I tried with following code. I got no result.
Qt Code:
ui.label21->setPalette(p);To copy to clipboard, switch view to plain text mode
I use Qt 4.1.1 under PLD Linux.Qt Code:
#include <QApplication> #include <QLabel> #include <QPalette> int main( int argc, char **argv ) { QLabel l; l.setPalette( p ); l.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
Whilst the example below or above works,
this doesn't:
Qt Code:To copy to clipboard, switch view to plain text mode
Qt Code:
#include "bwidget.h" #include <QLabel> { _myLabel->setPalette(p); _myLabel->setText("OK"); // setPalette(p); }To copy to clipboard, switch view to plain text mode
After the window is redrawn, the background colour of the label is displayed as desired for a split second (e.g. after it was hidden behind another window and it's shown again. You have to toggle very fast to see it, though).
But something redraws it with the standard gray.
Any ideas?
![]()
IMO this is a Qt bug (I use Qt 4.1.1 under PLD Linux). One can reproduce it even in Qt Designer (just create a form with a label, change its background colour with palette editor and see the preview).
mmh, really looks like a bug.
I played around with it a bit and whilst your example works even if modified (using the background image) mine doesn't work.
Somewhere in the doc is an example how to transform the QT3 setPaletteBackgroundPixmap to QT4.
Using QT += qt3support int the pro file and a Q3TextEdit the background image or colour still shows up.
So if it's a bug. Is Trolltech aware of it?
Qt Code:
#include "bwidget.h" #include <QLabel> #include <QTextEdit> #include <Q3TextEdit> #include <QPalette> #include <QPixmap> #include <QApplication> #include <QVBoxLayout> { _myLabel->setText("QLabel"); anEdit->append("QTextEdit"); anEdit->setFixedHeight(24); Q3TextEdit *a3Edit = new Q3TextEdit; a3Edit->append("Q3TextEdit"); layout->addWidget(_myLabel); layout->addWidget(anEdit); layout->addWidget(a3Edit); // p.setColor(QPalette::Window, Qt::red); p.setColor(_myLabel->backgroundRole(), Qt::red); _myLabel->setPalette(p); anEdit->setPalette(p); a3Edit->setPaletteBackgroundPixmap ( pixmap ); // a3Edit->setBackgroundColor(Qt::red); // setPalette(p); setLayout(layout); }To copy to clipboard, switch view to plain text mode
(Using Qt 4.1.1 self compiled on Linux - SuSE 9.3)
I couldn't find anything similar in the task tracker, so it will be better to ask the Trolls.Originally Posted by Moppel
My previous example was incorrect.
For the background of the text widget you need to set QPalette::Brush.
So the example below works correctly for the textedit.
Uncomment either the setColor or the setBrush to see the picture or the colour in the background of the edit.
No matter that you uncomment for the label, no changes can be seen.
That is so for qt 4.1.1 on Windows, SuSE 9.3, SuSE 10.0 and qt 4.1.0 on SuSE 9.3 and 10.0.
Qt Code:
#include "bwidget.h" #include <QLabel> #include <QTextEdit> #include <QPalette> #include <QPixmap> #include <QApplication> #include <QVBoxLayout> { _myLabel->setText("OK"); anEdit->append("an edit"); layout->addWidget(_myLabel); layout->addWidget(anEdit); // p.setBrush(QPalette::Window, QBrush(pixmap)); // p.setColor(_myLabel->backgroundRole(), Qt::red); _myLabel->setPalette(p); // p.setColor(QPalette::Base, Qt::red); anEdit->setPalette(p); // setPalette(p); setLayout(layout); }To copy to clipboard, switch view to plain text mode
Bookmarks