Chapter 8. Buttons, Checkboxes and Radio Buttons

Every developer has faced this: the interface seems to work, buttons click, but behavior feels “wooden”, logic seems unclear, and code quickly turns into a random collection of signals and slots.

This chapter reveals why buttons in Qt aren’t just click elements, but full-fledged participants in interface architecture. You’ll discover how the same basic mechanism controls buttons, checkboxes, and radio buttons, and why professional developers build UI logic around it.

We’ll demonstrate key capabilities of QAbstractButton, working with on/off and tristate states, plus practical techniques for grouping interface elements. Just a few basic signals, but dozens of application scenarios that make code cleaner and maintenance easier.

This chapter includes ready-to-use code examples.

Chapter Self-Check

What role does the QAbstractButton class play and why do all button types inherit from it?Answer
Correct answer: QAbstractButton is the base class that implements common methods and functionality for all button types (QPushButton, QCheckBox, QRadioButton), such as setting text/images, interaction signals, and state queries.
What’s the fundamental difference between isDown() and isChecked() methods?Answer
Correct answer: isDown() returns true if the button is physically pressed at that moment, while isChecked() returns true if the button is in the checked state (for toggle buttons, checkboxes, and radio buttons), which persists after release.
Why can’t radio buttons (QRadioButton) be used individually and must be grouped?Answer
Correct answer: Radio buttons model a “one-of-many” relationship where only one option can be selected from a group. Selecting one radio button automatically deselects others, requiring grouping.
Why does a regular QPushButton need setCheckable(true) and what does it change?Answer
Correct answer: This method turns a regular button into a toggle button (switch) that can maintain two states—pressed and unpressed, similar to a room light switch.
When is the toggled() signal emitted and how does it fundamentally differ from clicked()?Answer
Correct answer: The toggled() signal is emitted when the toggle button’s state changes (on/off), while clicked() is emitted on every click regardless of state change. toggled() carries information about the new state.
What’s the difference between pressed() and clicked() signals, and when is it important to use pressed()?Answer
Correct answer: pressed() is emitted immediately when the mouse button is pressed, while clicked() is only emitted after release on the same button. pressed() is important for real-time reactions or long presses.
Why do checkboxes (QCheckBox) need a third indeterminate state Qt::PartiallyChecked?Answer
Correct answer: This state is used to display mixed states, for example when multiple files with different attributes are selected (like in Windows Properties dialog), or for three-level selection in hierarchies.
How do you programmatically set a checkbox to indeterminate state, and which method must be called first?Answer
Correct answer: First enable third state support with setTristate(true), then set the state with setCheckState(Qt::PartiallyChecked).
Why is it recommended to use other widgets for more than five checkboxes or radio buttons?Answer
Correct answer: Many checkboxes/radio buttons take up too much screen space. For checkboxes, it’s better to use QListWidget; for radio buttons—QComboBox (dropdown list), maintaining visibility advantage while saving space.
Which method creates a button with a popup menu and where is this used in modern interfaces?Answer
Correct answer: The setMenu() method attaches a QMenu object to the button. Examples include the Start button in Windows 11, buttons in Microsoft Edge. It’s an alternative to dropdown lists for compact action representation.
What happens in a QRadioButton group if the programmer doesn’t set any radio button to checked by default?Answer
Correct answer: All radio buttons will be unchecked, violating the “one-of-many” logic. Users can select any, but this is bad practice—always set one radio button active by default using setChecked(true).
What’s the role of setFlat(true) method for a button and where can this be useful?Answer
Correct answer: The method makes the button flat (outlines become invisible) while preserving functionality. Useful for creating “secret” buttons, icon buttons, or minimalist interfaces.
Why is the QGroupBox class needed beyond visual grouping, and what technical function does it perform for QRadioButton?Answer
Correct answer: QGroupBox not only visually groups elements but also creates a logical area where mutual exclusion logic works for radio buttons. It can also have a checkbox to control accessibility of the entire group and a shortcut key.

Hands-On Assignments

Easy Level

Button Control Panel
Create an application with four different button types: regular button, toggle button, flat button, and button with icon. When pressing the toggle button, output its current state (checked/unchecked) to console. The regular button should close the application.
Hints: Use setCheckable(true) to create a toggle button. For flat button—setFlat(true). Load an icon via QPixmap and set with setIcon() method. Connect the toggled(bool) signal to a slot for outputting state.

Medium Level

Text Display Settings
Create a window with QLabel displaying text and a settings panel with three checkboxes: “Bold”, “Italic”, “Underlined”. When any checkbox state changes, text should update instantly. Add a group of three radio buttons for font size selection: “Small (10pt)”, “Medium (14pt)”, “Large (18pt)”.
Hints: Use QFont for managing text style. Methods: setBold(), setItalic(), setUnderline(), setPointSize(). Connect clicked() signals from checkboxes and radio buttons to one slot that updates the font. Apply setFont() to QLabel.

Hard Level

Calculator with Modes
Create a calculator with buttons for digits 0-9 and operations (+, -, *, /). Add a radio button group for mode selection: “Standard”, “Scientific” (adds sin, cos, sqrt buttons), “Programmer” (adds AND, OR, XOR buttons). Use QGroupBox with checkbox to enable/disable operation history. When the checkbox is off, all history elements should become disabled.
Hints: Use QGridLayout for button placement. Create a slot for dynamically adding/removing buttons when mode changes. For QGroupBox checkbox, use setCheckable(true) and toggled(bool) signal. Connect it to setEnabled() for the history widget. Store current operation and result in class variables.

💬 Join the Discussion!

Figured out when to use checkboxes versus radio buttons? Have questions about toggled() vs clicked() signals?

Share your solutions to hands-on assignments, discuss nuances of button grouping, or ask questions about creating interactive interfaces!

Leave a Reply

Your email address will not be published. Required fields are marked *