Merge pull request #949 from jp2images/fix-menubar-test-macos

Make the menu bar keyboard test pass on macOS
This commit is contained in:
Laurent Trinques
2026-09-19 20:20:58 +02:00
committed by GitHub
+26
View File
@@ -21,6 +21,12 @@
#include <QMenuBar>
#include <QShortcut>
#ifdef Q_OS_MACOS
// Exported from QtGui but declared in qkeysequence.h only for the
// documentation build; Qt's own docs say to declare it like this.
Q_GUI_EXPORT void qt_set_sequence_auto_mnemonic(bool b);
#endif
/*
F10 should open the menu bar, as it does in most applications and as
someone working without a mouse will expect. Qt provides this on Windows
@@ -38,6 +44,7 @@ class TstMenuBarKeyboard : public QObject
Q_OBJECT
private slots:
void initTestCase();
void altLetterOpensMenu(); // control -- must pass, or nothing below means anything
void plainF10DoesNothingInQt(); // the gap being filled
void shortcutOpensMenuBar(); // the mechanism QETMainWindow uses
@@ -48,6 +55,13 @@ namespace {
QMainWindow *makeWindow(QMenu **file_menu)
{
auto *w = new QMainWindow;
#ifdef Q_OS_MACOS
// A QMenuBar is native on macOS: its menus live in the system menu
// bar, outside the Qt widget tree, where QTest key events never reach
// them and QMenu::isVisible() stays false. The in-window bar runs the
// same QMenuBar code the other platforms use.
w->menuBar()->setNativeMenuBar(false);
#endif
*file_menu = w->menuBar()->addMenu(QStringLiteral("&File"));
(*file_menu)->addAction(QStringLiteral("Quit"));
w->menuBar()->addMenu(QStringLiteral("&Edit"))->addAction(QStringLiteral("Copy"));
@@ -60,6 +74,18 @@ QMainWindow *makeWindow(QMenu **file_menu)
}
/*
macOS has no Alt+letter menu mnemonics, so Qt does not turn "&File"
into a shortcut there (qt_set_sequence_auto_mnemonic is off). The
control below needs one; switch mnemonics on for this process.
*/
void TstMenuBarKeyboard::initTestCase()
{
#ifdef Q_OS_MACOS
qt_set_sequence_auto_mnemonic(true);
#endif
}
/*
The control. Alt and a menu's letter is known to work, so if this fails
the environment cannot open menus at all and the other two tests are