Python Forum
QMenuBar mouse tracking
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QMenuBar mouse tracking
#1
In the example below, the menubar must be clicked before it begins tracking the mouse and opening menus. Does anyone know of a way that I can do this without the click? I would like hovering over a menu to open the menu without having to first click on the menubar.

from PySide6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        menu_bar = QMenuBar(self)
        self.setMenuBar(menu_bar)
        for a in "ABC":
            menu = QMenu(a, self)
            menu_bar.addMenu(menu)
            for b in "123":
                menu.addAction(a + b)


app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
Reply
#2
I can access enter and leave this way, but I haven't found a way to open the correct menu yet.
self.menu.popup always opens the last menu.

from PySide6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QWidget
from PySide6.QtCore import QEvent

class MyQMenu(QMenuBar):
    def __init__(self, *args, **kwargs):
        QMenuBar.__init__(self, *args, **kwargs)
        for a in "ABC":
            self.menu = QMenu(a, self)
            self.addMenu(self.menu)
            for b in "123":
                self.menu.addAction(a + b)

    def enterEvent(self, event):
        print("enter")
        #self.menu.popup(event.globalPos())
        
    def leaveEvent(self, event):
        print("leave")

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.menu_bar = MyQMenu()
        self.setMenuBar(self.menu_bar)


app = QApplication([])
window = MainWindow()
window.show()
app.exec()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad [PyQt] Stuttered Mouse Tracking Power_Broker 3 4,328 Nov-05-2020, 06:30 PM
Last Post: Power_Broker
  [PyQt] QPushButton and and QmenuBar issues zettai 5 4,709 Oct-28-2020, 07:55 AM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020