You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, April 20th 2011, 8:40pm

Set background color of a widget with Qt Style Sheets

I can set the background color successfully using QPalette, but I'm trying to use QStyleSheets everywhere. I've noticed problems between displays in Gnome vs Fluxbox with QPalette, and Qt Style Sheets seem to fix this problem.

How can I set the background color of a widget with a style sheet? I can tell you that none of these lines work:

Source code

1
2
3
4
setStyleSheet("* { background: black;}");
setStyleSheet("* { background-color: black;}");
setStyleSheet("QWidget { background: black;}");
setStyleSheet("QWidget { background-color: black;}");


Any ideas?

MasterBLB

Intermediate

  • "MasterBLB" is male

Posts: 188

Location: Poland/Wrocław

  • Send private message

2

Thursday, April 21st 2011, 8:06am

Well,I've checked command "QWidget { background-color: black;}" and it works (though I've used it on WinXP)
Search deeply the documentation about what system is supported by QT via style sheets.Also know that style sheets are unreliable as a longer practice shows,as well as they are very slow;
There are 10 kinds of people.Those who do understand the binary code,and those who do not ;)

3

Thursday, April 21st 2011, 2:57pm

AHA!!!!

Reading the documentation more closely (yes, shame on me the first time...) reveals the following important tidbit for QWidgets and StyleSheets:

Quoted

If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

void CustomWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
Adding this to my paintEvent fixed the problem.

It's interesting you say that style sheets are unreliable: can you give me some examples as to when? It seems like they're the only solution for cross-windows-manager graphics. We've had document-able issues with QPalette. And can you point me to where you found out they were very slow?

I'm not trying to doubt you at all: I just want to wrap my head around this before I take the plunge.

Thanks!

MasterBLB

Intermediate

  • "MasterBLB" is male

Posts: 188

Location: Poland/Wrocław

  • Send private message

4

Thursday, April 21st 2011, 3:15pm

Interesting,because my code worked without reimplementing paintEvent:

Source code

1
2
3
4
5
6
7
8
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QWidget w;
	w.show();
	w.setStyleSheet("QWidget { background-color: black;}");
	return a.exec();
}

but as I said it is on WinXP.

About those unreliability: in my former job we discovered very much problems with style sheets,such as aligning text on QLabels(?) and setting own fields defined by Q_PROPERTY.Also there were problems with detection of focused state (sometimes it worked,sometimes it didn't)...There were more,but I don't remember all the details now.
There are 10 kinds of people.Those who do understand the binary code,and those who do not ;)

5

Thursday, April 21st 2011, 3:49pm

My class wasn't simply a QWidget, it was a class which inherited from QWidget. That's probably why I needed the re-implemented paintEvent. Also, I already had some of paintEvent overloaded, so I'm guessing it clobbered the default function and broke the style sheet functionality.

Thanks for your help!