Issue
Asuming we have a QFormLayout
with objectName
set to "formLayout"
.
This layout consists of two QLabel
and QLineEdit
and objects:
formLayout = QtGui.QFormLayout()
formLayout.setObjectName("formLayout")
label1 = QtGui.QLabel()
label2 = QtGui.QLabel()
lineEdit1 = QtGui.QLineEdit()
lineEdit2 = QtGui.QLineEdit()
formLayout.addRow(label1, lineEdit1)
formLayout.addRow(label2, lineEdit2)
Now i want to change the style of the QLabel
's/QLineEdit
's in the formLayout
with an external .qss
file. I would expect something like this:
QFormLayout#formLayout > QLabel, QLineEdit{
/*set some styles for QLabel/QLineEdit in the formLayout*/
}
Unfortunatly this isn't working. I know i could set all QLabel
's in formLayout
to the same object name:
label1 = QtGui.setObjectName("formLayoutLabel")
label2 = QtGui.setObjectName("formLayoutLabel")
and set their style like this:
QLabel#formLayoutLabel{
/*set some styles for QLabel in the formLayout*/
}
My Question:
How can I change the style of all widgets of a specific type (in example QLabel
and QLineEdit
) located in a parent layout/widget (in example formLayout
) within a external .qss
without using object name property?
Solution
QLayout
derived classes are not QWidget
derived, which leave them out of the Qt style system altogether.
A way to solve the issue is to back each layout with a widget, i.e. don't let a widget have more than one layout. This way all the widgets (i.e. labels) belonging to the layout are also children of the same widget, in which you can inject the stylesheet:
QLabel {
/*set some styles for QLabel in the formLayout*/
}
So, if you have a widget with, say, four layouts, you should put four children widgets in it and put each layout inside one of them (then lay out the widgets the same way you previously arranged the layouts).
An alternative workaround consists of subclassing the widgets and, in the stylesheet, select them by type name. For example, if you have special labels in a layout, use the simplest possible QLabel
subclass:
class Layout1Label(QtGui.QLabel):
def __init__(self, parent=None):
super(Layout1Label, self).__init__(parent)
then add only labels of that type to the layout:
label = Layout1Label()
layout.addWidget(label)
and refer to them in the stylesheet:
Layout1Label {
/*set some styles for all labels in layout*/
}
This should be effective, but can get tedious and require much effort (also you could end up with a lot of types, only meant for the stylesheet selection sake).
Answered By - p-a-o-l-o
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.