pytest-dev / pytest-dev/pytest-qt
BUG: Click Away from MenuBar does not Close MenuBar
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 455
- Forks
- 72
- Avg merge
- 12h 26m
- Merged PRs (30d)
- 1
Description
System
OS: Windows 10
Python: 3.8.10
Qt: PySide6
pytest-qt: 4.0.2
Problem
After clicking a QMenuBar item, clicking away from the menu does not close it when using qtbot.
MRE
myproj/main.py
from qtpy import QtWidgets
class View(QtWidgets.QMainWindow):
"""The main window."""
def __init__(self):
super().__init__()
self.container = QtWidgets.QFrame()
self.layout_ = QtWidgets.QVBoxLayout()
self.layout_.setSpacing(0)
self.layout_.setContentsMargins(0, 0, 0, 0)
self.container.setLayout(self.layout_)
self.setCentralWidget(self.container)
self._create_actions()
self._create_menubar()
def _create_actions(self):
self.new_action = QtWidgets.QAction(
'&New Project...',
self,
)
def _create_menubar(self) -> None:
self.menubar = self.menuBar()
self.file_menu = self.menubar.addMenu('&File')
self.file_menu.addAction(self.new_action)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = View()
window.showMaximized()
app.exec_()
tests/test_window.py
import pytest
from myproj.main import View
from pytestqt.qtbot import QtBot
from qtpy import QtCore
@pytest.fixture(name='window', scope='function')
def fixture_app(qtbot):
window = View()
window.setMouseTracking(True)
window.showMaximized()
yield window
def test_click_outside_menu_closes_menu(window: 'View', qtbot: 'QtBot') -> None:
"""Tests that menu closes when user clicks away from it.
Args:
window (view): (fixture) Qt application main window
"""
menubar = window.menubar
file_menu = window.file_menu
new_action = window.new_action
file_rect = menubar.actionGeometry(file_menu.menuAction())
new_rect = file_menu.actionGeometry(new_action)
pause = 1000 # milliseconds
# Assert - Precondition
assert not file_menu.isVisible()
# # Act - Menubar
qtbot.wait(pause)
qtbot.mouseMove(menubar, file_rect.center())
qtbot.wait(pause)
qtbot.mouseClick(menubar, QtCore.Qt.LeftButton, pos=file_rect.center())
qtbot.wait(pause)
qtbot.mouseMove(file_menu, new_rect.center())
qtbot.wait(pause)
qtbot.mouseMove(window, window.rect().center())
qtbot.wait(pause)
qtbot.mouseClick(window, QtCore.Qt.LeftButton, pos=window.rect().center())
qtbot.wait(pause)
# Assert - Precondition
assert not file_menu.isVisible()
Result

Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied reproduction in tests/test_window.py with the listed Windows, Python, Qt, and pytest-qt versions, then inspect pytest-qt's handling of qtbot.mouseMove and mouseClick. Done means the final assertion passes and the QMenuBar menu is hidden after clicking outside it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- desktop, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100