views:

45

answers:

2

I'm new to Qt and have been designing forms using Qt Creator. I've noticed that I can apply styles for specific widgets in the form's stylesheet, and it will style every widget on the form. For example "QPushButton{color: red;}" will make all the QPushButtons on the form have red text.

Is there a way to only apply styles to certain groups of widgets? For example, if I promote a QPushButton and call it MyButton, how can I set styles only for MyButtons. I would expect to be able to do something like "MyButton{color: green;}" but that doesn't seem to work.

Is there a better way to do what I'm trying to do (preferably using Qt Creator)? I can't seem to find an example of this anywhere, but maybe it's because I'm not using the correct terminology.

Thanks, Mark

+2  A: 

IIRC that should work, but creator might not show the preview correctly as it is not instantiating your button. Personally I think working with a global .css file that gets loaded at startup works better than applying single styles on each element in creator.

Use QApplication::setStyleSheet() to set a global stylesheet

Harald Scheirich
+1 for suggesting a single style sheet.
Arnold Spence
Yes, I'll definitely use a single style sheet, thanks!
Mark McEver
+1  A: 

There are a lot of options for specifying selectors in style sheets. They are documented here.

One that I use (that is hinted to in the official style sheet reference document) is |=

QPushButton[objectName|="somePrefix_"]

I use this to select items that have the same object name prefix. You could of course achieve a similar thing by applying a stylesheet to a panel containing the buttons you want to style differently but this can get tricky.

Arnold Spence
That's exactly what I was looking for, thanks!
Mark McEver