Group Radio Buttons in Qt Creator

How to group radio buttons in Qt Creator in a Qt Widgets Application. This article shows the different ways to group radio buttons on a window in Qt Creator so that they behave like a group.

Group sets of radio buttons as separate groups. Each group operates independently from the other groups. The following image shows the various ways used to group radio buttons in this tutorial.

How to Group Radio Buttons in Qt Creator
How to Group Radio Buttons in Qt Creator

Part 11 of the Qt Creator C++ Tutorial

Go to the Qt Programming Tutorial Series Table of Contents

Group Radio Buttons in Qt Creator Tutorial Steps

Follow the tutorial steps below to build a new application in Qt Creator. Then use various ways to group radio buttons together so that they behave as a unit.

Watch the video embedded above to see these steps done in Qt Creator.

1. Start a New Project

Start Qt Creator and then start a new Qt Widgets Application project. Call the project radio_button_group. It is assumed that you have been following this Qt Creator C++ tutorial series and know how to create a new project.

2. Place Radio Button on the Main Window

Find Radio Button under the Buttons section in the left pane of Qt Creator. Drag and drop three radio buttons onto the main window. Place them one above the other.

Save the changes and run the application. Notice that the radio buttons are automatically linked as a unit or group. Only one button in the group can be checked at a time.

3. Group Radio Buttons with Vertical Layout

Place three more radio buttons on the main window. Stack the buttons vertically next to the first set of buttons. Run the application and note that the new radio buttons are part of the first set. Out of the six radio buttons, only one can be checked at a time.

3.1 Group the Radio Buttons Vertically

Group the two sets of three radio buttons using a Vertical Layout widget. To do this, first select a group of three radio buttons. This can be done by left-clicking and dragging, or by clicking one button and then holding down the Ctrl key and clicking the other two buttons. Click the Lay Out Vertically button on the top toolbar.

3.2 Group the Second Set of Radio Buttons

Do the same for the other group of buttons. Save the layout. Run the application. See that the two groups of buttons are now two separate units. Each group can have only one button checked at a time.

4. Group Radio Buttons with Horizontal Layout

Add three more radio buttons to the main window. This time select all buttons and click the Lay Out Horizontally icon on the top toolbar. Note that this group behaves as a separate unit from the other groups.

5. Select and Group Radio Buttons

Radio buttons can be grouped as a unit without using one of the layout widgets. Place two more groups of three radio buttons on the main window.

Select one group of three radio buttons. Right click on one of the selected radio buttons. On the menu that pops up, select Assign to button group → New button group. Do the same for the second button group.

Run the application. Note that the two new button groups behave as separate units.

6. Set the Default Checked Radio Button for a Group

Select any button from a group of radio buttons. Usually this is the top or left button. In the right pane of Qt Creator, look under QAbstractButton. Find the item called checked. Click the checked item to check it. This sets the selected button as the default selected radio button.

Leave a Reply

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