2

I would like to set two things for submenus in PyQt:

  1. not open submenus when hovered
  2. no delay when submenus get opened by a click

I suppose that I had to modify the hover behavior of the QAction object that is returned by the menuAction() method of the submenu object - but how to do this?

There is a QStyle::SH_Menu_SubMenuPopupDelay setting mentioned in the docs that is maybe what I need for the second but I also have no clue how to set this in PyQt.

My basic menu example:

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

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.menubar = QtGui.QMenuBar(self)
        self.setMenuBar(self.menubar)
        self.menuFile = QtGui.QMenu(self.menubar, title='File')
        self.menubar.addAction(self.menuFile.menuAction())
        self.submenu = QtGui.QMenu(self.menuFile, title='Submenu')
        self.menuFile.addAction(QtGui.QAction(self, text="First"))
        self.menuFile.addAction(self.submenu.menuAction())
        self.menuFile.addAction(QtGui.QAction(self, text="Third"))
        self.submenu.addAction(QtGui.QAction(self, text="First"))
        self.submenu.addAction(QtGui.QAction(self, text="Second"))
        self.submenu.addAction(QtGui.QAction(self, text="Third"))

if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())
TNT
  • 3,392
  • 1
  • 24
  • 27
  • I do not know of any easy way to achieve this. But I guess that subclassing QMenu and overriding MouseEvent methods might be an option. See also this answer http://stackoverflow.com/a/28266541/1536976 – NoDataDumpNoContribution Feb 02 '15 at 11:44

0 Answers0