This chapter gradually reveals that a menu is not just a set of items, but an interaction scenario. Here you’ll discover how professional applications achieve a “native” interface feel without breaking platform conventions. You’ll learn the secrets that allow you to shorten the user’s path to action while simultaneously simplifying code maintenance.
Several menu types will be covered, practical techniques for working with QMenu and QMenuBar will be shown, along with the logic of using mnemonics and keyboard shortcuts. Real-world examples demonstrate how proper menu structure reduces cognitive load and enhances the perception of professional interface quality.
Skipping this chapter risks staying at the “it works, but it’s awkward” level. And users notice such details faster than anything else.
This chapter includes ready-to-use code examples.
Chapter Self-Check
What’s the fundamental difference between keyboard shortcuts and mnemonics (access keys)?Answer
Correct answer: Keyboard shortcuts are global combinations (e.g., Ctrl+S) that work anywhere in the application, while mnemonics are contextual Alt+letter combinations that require first opening the relevant menu.
Why shouldn’t top-level menu commands be used to directly execute actions?Answer
Correct answer: Top-level commands are intended exclusively for displaying dropdown menus; using them for other purposes violates familiar interface patterns and confuses users.
What happens to the & symbol before a letter in a menu command name on macOS?Answer
Correct answer: The & symbol is simply removed from the text, since macOS doesn’t support mnemonics by platform design; underlines in menu items aren’t displayed, and Alt+letter navigation doesn’t work.
What role does the QAction class play in Qt’s menu system?Answer
Correct answer: QAction represents a separate command (action) in the menu, encapsulating its properties (text, icon, keyboard shortcuts, state) and can be reused in different parts of the interface (menus, toolbars).
Why is it recommended to limit submenu nesting to a maximum of two levels?Answer
Correct answer: Deep nesting complicates navigation and makes menus less intuitive for users; two levels is the optimal balance between command organization and usability.
Why don’t mnemonic underlines display by default in Windows 10 and 11, even when pressing Alt?Answer
Correct answer: Microsoft evolved the visual design of the interface for greater clarity, while the Alt+letter mnemonic functionality is fully preserved regardless of visual display.
Which method should be called to show a context menu in the correct position when the right mouse button is pressed?Answer
Correct answer: You need to call menu->exec(event->globalPos()) in the contextMenuEvent() method, passing the global mouse pointer coordinates from the event object.
How do you make a menu command unavailable (displayed in gray)?Answer
Correct answer: Via the QAction pointer, call the setEnabled(false) method, which will visually display the command in gray and block its execution.
Why add an ellipsis at the end of a menu command name?Answer
Correct answer: An ellipsis signals to the user that selecting the command will open a dialog box for additional actions; this doesn’t apply to simple message boxes.
What does the addAction() method return after adding a command to a menu?Answer
Correct answer: The method returns a pointer to a QAction object, through which you can configure the command’s properties (checkable state, icon, availability, keyboard shortcuts).
How does a context menu fundamentally differ from a popup menu in Qt?Answer
Correct answer: A context menu uses the same QMenu class but isn’t attached to QMenuBar and is shown via exec() when handling a right-click mouse event.
Which signal does a menu emit when the user selects any command, and what does it pass?Answer
Correct answer: The menu emits the triggered(QAction*) signal, passing a pointer to the action object of the selected command, which allows identifying the command in a single slot.
Why does Qt automatically adapt mnemonic display to the specific operating system?Answer
Correct answer: Different OSes have different conventions for displaying access keys; automatic adaptation ensures a native application appearance and meets user expectations on each platform.
Practice Assignments
Easy Level
Text Style Selection Menu
Create an application with a text field (QTextEdit) and a “Style” menu with commands to change text formatting: Bold, Italic, Underline. Each command should have a checkbox reflecting the current state. Add keyboard shortcuts: Ctrl+B, Ctrl+I, Ctrl+U respectively. Include a separator and a “Reset Formatting” command at the end.
Hints: Use QMenuBar and QMenu. For checkboxes, call setCheckable(true) on QAction objects. QTextEdit methods: setFontWeight(), setFontItalic(), setFontUnderline(). For reset, create a new QTextCharFormat and apply it via setCurrentCharFormat().
Medium Level
Editor with Context Menu and Color Schemes
Develop a text editor with a top-level “File” menu (New, Open, Save, Exit) and a context menu for changing color schemes. Right-clicking on the text field should display a menu with a “Theme” submenu containing options: Light, Dark, Blue. Theme selection should change background and text colors. Add mnemonics for all commands and standard keyboard shortcuts for file menu commands.
Hints: Override contextMenuEvent(). Create a submenu via addMenu(). Use QPalette for changing colors or setStyleSheet(). The triggered() signal passes QAction*, from which you can get text() to determine the selected theme. Don’t forget to add an ellipsis to “Open…” and “Save…” commands.
Hard Level
Dynamic Recent Files Menu
Create an image viewer application with a full menu. Implement a dynamic “Recent Files” submenu in the “File” menu that stores the last 5 opened files. Selecting a file from the list should open it. The list should update with each new file opened (most recent on top). If a file already exists in the list, move it to the top. Add a “Clear List” command at the end of the submenu. Recent file commands should be disabled if the file has been deleted from disk. Save the list between application runs.
Hints: Use QSettings to save the file list (QStringList). Create a slot to update the submenu, called each time a file is opened. Check file existence via QFile::exists(). For dynamic menus, use clear() and re-add actions. Connect each action to a single slot that passes the file path via QAction::setData() and sender(). Use QFileDialog for file selection, QLabel with QPixmap for displaying images.
💬 Join the Discussion!
Got a handle on creating menus in Qt? Have questions about the differences between keyboard shortcuts and mnemonics?
Perhaps you’ve developed ideas for organizing complex menus or encountered peculiarities in context menu behavior across different platforms?
Share your experience, ask questions, or help other readers master the nuances of working with QMenu and QMenuBar!