Get Qt Widget variable name (for use in stylish sheet)?

In my application, the user clicks on any widget of my program (which is inactive at that time) and selects a color for it.
This color will be added to the stylesheet for this particular widget.

However, when the program ends and starts again, I would like this particular widget to retain its stylesheet.
I would like there to be no hard code in the style sheets for each widget. In fact, I would not even know which particular widget has a stylesheet.

I would really like for me to have one stylesheet for the application, as well as a new color code only for the selected widget.

(ie if the user clicked the QPushButton button and selects the style sheet {color: red},
I would like it to be just QPushButton, and no one else.
So, if this QPushButton has the variable name "Clicky",
in the QApplications stylesheet, I would add:
'QPushButton # Clicky {color: red}')

To do this and don’t need to hardcode it for each widget,
I have to somehow convert the variable name of my PyQt4 widgets to strings.
How can i do this?

Thanks!

(I read that getting python variable names from their values ​​is very difficult.
Is there any other form of identifier for the widget that can be added to the stylesheet?)


PyQt4
python 2.7.2
Windows 7

+3
variables qt pyqt pyqt4
source share
1 answer

You need to first set the object_name ("somename") before the object name, then objectName () will work or even better - findChild () or findChildren ()

Example

Title:

QButton foo; 

Grade:

 foo = new QButton(); foo.setObjectName("MySuperButton"); 

Then finally in your QSS ..

 #MySuperButton { background: black; } 

It also works similarly to CSS with

 QButton#MySuperButton { background: red; } 

The logic is why you want to set multiple object names in the same way (for different objects) or use the granularity of only one type of widget with a specific name, also almost the same as CSS.

+6
source share

All Articles