Issue
How do I change the color of the tooltip message? The stylesheet is not recognized. This Python class draws a toolbar and adds itself to the parent window
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.toolbar = QToolBar("toolbar")
self.toolbar.setIconSize(QSize(16, 16))
connect_action = QAction(QIcon('icon:icons8_connect_50.png'), "&Connect", self.parent)
connect_action.setCheckable(True)
connect_action.triggered.connect(self.connect_button)
self.toolbar.addAction(connect_action)
connect_action.setToolTip('Connect')
connect_action.setStyleSheet("QToolTip {color:rgb(0, 0, 0);"
" background-color: rgb(255, 255, 204);"
" border: 2px solid black;"
" font-family: Times New Roman;}")
I created a toolbar with multiple QAction items, each with an associated icon. Since QAction is not a widget, I couldn't apply a stylesheet to the button. How can I add a stylesheet when the item is not a widget but a QAction?
Updated
Solution
Stylesheets can only be applied to widgets, and QActions are not widgets: they are abstract objects that may eventually be shown on widgets that are able to display actions, but that can happen in very different ways.
For instance, an item in a QMenu, an icon in QLineEdit, or a button in QToolBar. In reality, actions can be used even if they are never shown.
This means that the possibility of using style sheets that are able to target actions (and especially specific actions) completely depends on the widget that displays them.
For example, QMenu only allows to change the appearance of all its actions, not specific ones.
Luckily, when an action is added to a QToolBar, it automatically creates a QToolButton for it (except when using a QWidgetAction that returns a different widget type).
QToolBar provides the possibility to retrieve the widget related to its actions by using widgetForAction()
, meaning that we can set individual stylesheets for each button:
self.toolbar.addAction(connect_action)
toolButton = self.toolbar.widgetForAction(connect_action)
toolButton.setStyleSheet('...')
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.