Issue
I would like to use within PyQt5 a style sheet
to layout custom widgets. Generally this works: the widgets are displayed with the modified properties. However the property of the widget (e.g. style-sheet font-family
vs. QLabel.label()
) seems to stay the default value (s. minimal example below).
Is the style sheet only used for painting the widget and does not change the widget itself?
Is there a way to get the 'new' properties? Otherwise using for example FontMetrics
in combination with a style sheet
is not possible?
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QFontInfo
stylesheet = '''
QLabel{
font-family: Wingdings;
}
'''
app = QApplication(sys.argv)
label = QLabel('Hello World!')
label.setStyleSheet(stylesheet)
print(QFontInfo(label.font()).family()) # prints out: MS Shell Dlg 2; expected: Wingdings
label.show()
app.exec()
Solution
Your suspect is indeed correct: when using stylesheets, many widget properties are ignored, both for their getters and setters.
There is no direct way to know those values, as they all are managed internally by Qt's parser.
Most importantly, the palette()
and font()
(as fontMetrics()
, obviously) are not affected, so you cannot know what are the font properties or the palette colors used for the stylesheet, and if you try to set the font or set a new palette you'll get no result (all this assuming that you're actually setting some color or font in the stylesheet, obviuosly).
This is also valid for other widget specific properties (for example, the QFrame border style), which might even have slightly different results on different styles/systems.
An this is also the reason for which you should either use stylesheets or a QPalette, as mixing them might result in unexpected behavior.
From QWidget.palette()
docs:
Warning: Do not use this function in conjunction with Qt Style Sheets.
From QWidget.font()
docs:
Note : If Qt Style Sheets are used on the same widget as setFont(), style sheets will take precedence if the settings conflict.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.