Search code examples
python-2.7menupyqtpyqt5statusbar

PyQt5 Statusbar gets hidden when hovering over Menu item


I´m trying to learn PyQt5 through a tutorial. I´m using Python 2.7 and PyQt5.

This code should create a menubar with a "View" entry. Inside the "View" entry you should be able to check or uncheck an option to show or hide the statusbar below.

import sys
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Example(QMainWindow):

    def __init__(self):
        super(Example,self).__init__()

        self.initUI()


    def initUI(self):         

        self.statusbar = self.statusBar()
        self.statusbar.showMessage('Ready')

        menubar = self.menuBar()
        viewMenu = menubar.addMenu('View')
        viewMenu.menuAction().setStatusTip("File Menu is hovered")

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

        self.statusbar.addPermanentWidget(self.myMessage)

        viewStatAct = QAction('View statusbar', self, checkable=True)
        #viewStatAct.setStatusTip('View statusbar')
        viewStatAct.setChecked(True)
        viewStatAct.triggered.connect(self.toggleMenu)

        viewMenu.addAction(viewStatAct)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Check menu')    
        self.show()

    def toggleMenu(self, state):

        if state:
            self.statusBar().show()
        else:
            self.statusBar().hide()


if __name__ == '__main__':
    app=0
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

The problem is that, however, when I move the mouse over the "View" entry the statusbar disappears completely and doesnt react to any clicks on the menubar like it is supposed to do.

I have looked at other tutorials, questions in SO and the documentation page to no avail.

Any ideas? Thanks in advance!


Solution

  • Assuming you always want some default status text displayed, adding the following method to your class should do the trick.

    def event(self, e):
        if e.type() == QtCore.QEvent.StatusTip:
            if e.tip() == '':
                e = QtGui.QStatusTipEvent('Ready')  # Set this to whatever you like
        return super().event(e)