Issue
I'm learning PySide6 and I stumbled upon a weird thing. When creating a QAction and setting the status tip, the name of the QAction is shown as a status tip instead of the actual status tip.
What am I missing here?
Here is my short example code:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Test App")
label = QLabel("Hello!")
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
toolbar = QToolBar("My main toolbar")
self.addToolBar(toolbar)
button_action = QAction("Test", self)
button_action.setShortcut('Ctrl+T')
button_action.setStatusTip('Test application')
button_action.triggered.connect(self.onMyToolBarButtonClick)
toolbar.addAction(button_action)
def onMyToolBarButtonClick(self, s):
print("click", s)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Here is the weird result:
Thank you!
Solution
What is shown in the image is a tool tip, and by default a tool bar shows the action's text()
as a tooltip, unless a tool tip is explicitly set using setToolTip()
.
The status tip, instead, is shown in the status bar (a QStatusBar).
On a QMainWindow the status bar can be accessed using statusBar()
(if none exists, which is the default for new empty main windows, a new one is created and returned).
Just add the following anywhere in the __init__()
and you'll see that the "Test application"
string is actually shown there when hovering the action:
self.statusBar()
A status bar can also be installed on any QWidget or inherited subclass, and can be used to capture any status event received from itself or its children:
class MainWindow(QWidget): # note: a basic QWidget
def __init__(self):
super().__init__()
layout = QVBoxLayout(self)
button = QPushButton('Test')
layout.addWidget(button)
button.setStatusTip('Test application')
self.statusBar = QStatusBar()
layout.addWidget(self.statusBar)
def event(self, event):
if event.type() == event.Type.StatusTip:
self.statusBar.showMessage(event.tip())
# returning True means that the event has been
# successfully handled and will not be propagated to
# the possible parent(s)
return True
return super().event(event)
The above is what actually QMainWindow does under the hood.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.