Issue
I want to add images to my buttons. I have all my styling in a separate file.
According to Qt docs https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types I should be able to access a specific button using the following syntax
QPushButton#my_button_name {}
but it does not seem to work. Here is an example from my code:
# script with widgets and layouts
class Tab1():
self.button1 = QPushButton()
# styling script
def button_style():
return '''QPushButton#button1 {font:15px;}'''
I am misunderstanding something? I tried adding 'self.' to the name in the styling script but it does not work either.
Solution
Name selectors for stylesheets must be used by setting the objectName property; python attribute naming is completely useless for this as Qt doesn't know anything about it.
In order to correctly apply the stylesheet as you want to, you need to set the object name:
# ...
self.button1 = QPushButton()
self.button1.setObjectName('button1')
self.setStyleSheet('QPushButton#button1 {font:15px;}')
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.