QStatusBar message disappears on menu hover
Asked Answered
P

5

7

I have a very basic QMainWindow application that contains a menubar and a statusbar. When I hover over the menu the status message disappears. More precisely, the status message is cleared. I have no idea what is causing this behavior but it's resulting in a very difficult workaround for what I hoped to be trivial behavior.

This is problematic for the following reason: I can make the message permanent by adding a QLabel widget to the QStatusBar, but then I get the awkward border. I don't want the border. The only way I know how to remove the border is via QStatusBar.setStyleSheet(). I am using a palette for my color scheme as opposed to a stylesheet so modifying the stylesheet messes up other colors. I also can't restore the original statusBar QLabel color when I make a modification via the stylesheet. I'm not the best at using stylesheets.

Is there a way to prevent the menu interaction from clearing the status message? If not, is there a way to remove the border from the StatusBar when adding a QLabel widget while preserving my palette (maybe not via stylesheets)?

#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class win(QMainWindow):
    def __init__(self,parent=None):
        super(win,self).__init__(parent)
        self.menubar = QMenuBar(self)
        self.fileMenu  = QMenu("File")
        self.exitAction = QAction("Exit",self)
        self.fileMenu.addAction(self.exitAction)
        self.menubar.addMenu(self.fileMenu)   
        self.statusBar().showMessage("Hello")
        self.connect(self.exitAction,SIGNAL("triggered()"), self.close)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    GUI = win()
    GUI.show()
    app.exec_()
Pirate answered 11/7, 2014 at 10:4 Comment(0)
G
4

Basically, each widget you hover over sets the status bar text to their statusTip property even when that property is an empty string.

For QMenu, the text is stored in the menuAction action status tip, so, you can have a text instead of just clearing the status bar with something like this:

self.fileMenu.menuAction().setStatusTip("File Menu is hovered")

To prevent anything to change the status bar, you can probably install an eventFilter on the status bar and filter out all QStatusTipEvent.

Garcia answered 11/7, 2014 at 12:43 Comment(2)
Is this specific to QMainWindows? I did not notice this behavior when I used a status bar with a QDialog. I didn't have a menu so maybe that's why.Pirate
@user1054424 Yes, it is specific. The QStatusTipEvent is sent even for hovered widgets in a QDialog but it isn't handled by anything by default.Garcia
P
8

I got the same problem, and I found another way which is creating a new QLabel

self.myMessage = QtGui.QLabel()
self.myMessage.setText("Hello")

and add it as an widget to the status bar on the left

self.statusBar.addWidget(self.myMessage)

or on the right

self.statusBar.addPermanentWidget(self.myMessage)
Pas answered 14/6, 2016 at 22:12 Comment(0)
G
4

Basically, each widget you hover over sets the status bar text to their statusTip property even when that property is an empty string.

For QMenu, the text is stored in the menuAction action status tip, so, you can have a text instead of just clearing the status bar with something like this:

self.fileMenu.menuAction().setStatusTip("File Menu is hovered")

To prevent anything to change the status bar, you can probably install an eventFilter on the status bar and filter out all QStatusTipEvent.

Garcia answered 11/7, 2014 at 12:43 Comment(2)
Is this specific to QMainWindows? I did not notice this behavior when I used a status bar with a QDialog. I didn't have a menu so maybe that's why.Pirate
@user1054424 Yes, it is specific. The QStatusTipEvent is sent even for hovered widgets in a QDialog but it isn't handled by anything by default.Garcia
E
4

Just to update Lazywii's answer regarding using a QLabel. That code didn't work exactly as is so maybe there have been some changes since 2016 but what did work in 2020 on PyQt5 is:

    self.myMessage = QtWidgets.QLabel()
    self.myMessage.setText("My message not affected by tooltips from hovering")

    self.statusbar.addWidget(self.myMessage)
Eldwun answered 26/1, 2020 at 0:2 Comment(0)
S
2

One complete example

# Created by [email protected] at 2022/2/15 22:27
from PySide2 import QtWidgets, QtCore, QtGui


class StatusTipFilter(QtCore.QObject):
    def eventFilter(self, watched: QtCore.QObject, event: QtCore.QEvent) -> bool:
        if isinstance(event, QtGui.QStatusTipEvent):
            return True
        return super().eventFilter(watched, event)


app = QtWidgets.QApplication()
window = QtWidgets.QMainWindow()
window.menuBar().addMenu("File")
window.statusBar().showMessage("Ready")
window.menuBar().installEventFilter(StatusTipFilter(window))
window.show()
app.exec_()
Semitone answered 15/2, 2022 at 14:41 Comment(0)
P
0

And to answer the portion about removing the border from the statusbar: self.statusbar().setStyleSheet("QStatusBar::item{ border: 0px solid black };") does the trick. It is important to setStyleSheet only on the statusbar object and not the entire application.

Pirate answered 12/7, 2014 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.