PyQt6: Popup QMainWindow sometimes ignoring ("missing") clicks even if opened and activated
-
I’m building a macOS menu bar extra (a.k.a. system tray) app using PyQt6. When clicking the menu bar icon, a popup window appears, which is a QMainWindow with the
Qt.WindowType.Popupwindow flag set.I’m noticing sporadic issues where the popup window doesn't respond to clicks even though it's open. Sometimes, clicking on a QPushButton inside the popup, or an action on the QMenu, causes it to close abruptly without the
clickedsignal being invoked on the button or thetriggeredaction on a menu item (i.e., clicks are sometimes being “missed”). The popup window contains a QTreeWidget which I’ve styled with QSS so that hovering over a QTreeWidgetItem will cause it to be highlighted; sometimes the highlight doesn’t appear, suggesting that the open popup window is ignoring user interaction. Switching between windows from other open applications and then clicking the menu bar icon for my app sometimes causes the issue to occur.The issue is reproducible on macOS 15 Sequoia and macOS 26 Tahoe, using a mouse (on a Mac Mini) and a trackpad (on a MacBook Air). I'm using PyQt6 version 6.11.0, and Python 3.14.4. The app is distributed as a PyInstaller build, but I've noticed this issue sometimes occurring on my dev machine, where the app is executed directly from the Python interpreter.
I tried calling the
activateWindow()method on the QMainWindow object for the popup window, as well as callinggrabMouse()(andreleaseMouse()in the overridden methodcloseEvent()). I also ensured that the widgets within the popup window have itsselfobject passed to their constructors. But the issue is still occurring.What should I be doing to ensure that the popup window is consistently capturing mouse input when it’s opened? What gotchas and caveats that I missed that could be causing this issue?
-
Hi and welcome to devnet,
Do you have the same issue if using PySide6 ?
Can you share a minimal script that shows this issue ? -
Issue is reproducible in PySide6. Below is the source code for a demo app; it should be executed on macOS 13 Ventura or newer. Also included is an image file to use as the menu bar extra icon.
Again, this is a sporadic issue, so you may have to try it a few times to cause the issue to occur. Switching between windows from other open applications and then clicking the menu bar icon for my app may cause it to happen.
demo.py:import sys from PySide6.QtWidgets import QApplication from SamplePopupWnd import SamplePopupWnd mainwnd = None def main(): global mainwnd app = QApplication(sys.argv) app.setQuitOnLastWindowClosed(False) mainwnd = SamplePopupWnd() sys.exit(app.exec()) main()SamplePopupWnd.py:import sys from PySide6.QtCore import Qt, QPoint, QSize from PySide6.QtGui import QIcon, QAction, QGuiApplication from PySide6.QtWidgets import (QWidget, QMenu, QSystemTrayIcon, QHBoxLayout, QPushButton, QMessageBox, QMainWindow) class SamplePopupWnd(QMainWindow): def __init__(self): super().__init__() self.popupwidth = 300 self.popupheight = 400 # Menu bar extra icon. icon = self.getSystemTrayIcon() self.systray = QSystemTrayIcon() self.systray.setIcon(icon) self.systray.setVisible(True) self.systray.activated.connect(self.togglePopup) self.button = QPushButton("button", self) self.button.clicked.connect(self.showButtonMessage) # Actions for a menu that is opened by a menu button self.settings = QAction("settings", self) self.settings.triggered.connect(self.showMenuMessage) self.about_act = QAction("about", self) self.about_act.triggered.connect(self.aboutActionSelected) self.quit_act = QAction("quit", self) self.quit_act.triggered.connect(self.quitActionSelected) self.menu = QMenu(self) self.menu.addAction(self.settings) self.menu.addAction(self.about_act) self.menu.addSeparator() self.menu.addAction(self.quit_act) self.menubutton = QPushButton("≡ menu", self) self.menubutton.setMenu(self.menu) hbox = QHBoxLayout(self) hbox.addWidget(self.button) hbox.addStretch(2) hbox.addWidget(self.menubutton) # Add to a QWidget that is styled with rounded corners. self.popup = QWidget(self) self.popup.setObjectName("popup") self.popup.resize(QSize(self.popupwidth, self.popupheight)) self.popup.setLayout(hbox) self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True) self.setWindowFlags(Qt.WindowType.Popup) self.setGeometry(200, 100, self.popupwidth, self.popupheight) self.setStyleSheet(""" #popup { border-radius: 16px; background-color: #fefefe; } """) def showButtonMessage(self): QMessageBox.information(self, "demo", "the Button was clicked!", QMessageBox.StandardButton.Ok) def showMenuMessage(self): QMessageBox.information(self, "demo", "the Menu Action was clicked!", QMessageBox.StandardButton.Ok) def aboutActionSelected(self): QMessageBox.about(self, "about", "qt for python popup demo 1.0") def quitActionSelected(self): sys.exit() def getSystemTrayIcon(self): icon = QIcon("systray.png") return icon def togglePopup(self, reason): # Handle click on system tray icon. if reason == QSystemTrayIcon.ActivationReason.Trigger: if self.isVisible(): self.close() else: self.openPopup() def openPopup(self): systray_geom = self.systray.geometry() x = systray_geom.x() y = 0 if systray_geom.y() < 0 else systray_geom.y() currscreen = QGuiApplication.screenAt(QPoint(x, y)) screen_avail_geom = currscreen.availableGeometry() top = screen_avail_geom.y() left1 = systray_geom.x() left2 = screen_avail_geom.width() - self.popupwidth # Align popup left coords with the system tray icon. left = left1 if left1 < left2 else left2 self.setGeometry(left, top, self.popupwidth, self.popupheight) self.show() self.activateWindow()systray.png:
