Find Selected Qt Radio Button

Find the selected Qt radio button in a set of radio buttons in a Qt 5 application. How to find the selected radio button in Qt using Qt Creator and C++.

This tutorial also shows how to copy and rename a Qt project created with Qt Creator. The following image shows the application built in this tutorial. Different methods are used to find the checked radio button. Firstly buttons are tested individually. Secondly the checked button is found in a group.

Find the Selected Qt Radio Button in a Group of Radio Buttons
Find the Selected Qt Radio Button in a Group of Radio Buttons

Part 13 of the Qt Creator C++ Tutorial

Go to the Qt Programming Tutorial Series Table of Contents

Qt Radio Button Tutorial Overview

Two projects are made in this tutorial. The first project is made by copying the project from the previous part of this tutorial series.

The first project finds the checked radio button when any radio button is clicked. In the second project, a button is clicked to read the state of the radio buttons.

Find Selected Radio Button Tutorial Steps

Below are all the tutorial steps to create the two radio button projects.

1. Copy the Previous Project

Follow the steps below to copy the previous project and rename it.

  1. Make a copy of the previous project folder – part 12 of this series.
  2. Rename the folder to radio_btn_read.
  3. Open the new project folder and delete the debug folder.
  4. Rename the folder inside the radio_btn_read folder to radio_btn_read.
  5. Open the new renamed folder.
  6. Rename the two files that have the old project name.
  7. Start Qt Creator and open the renamed project.
  8. Click the Configure button in Qt Creator.
  9. Open radio_btn_read.pro in Qt Creator and rename the TARGET to the new project name.

2. First Project to Find Selected Qt Radio Button

In this project the radio buttons are individually tested to see which one is checked.

  1. Delete the old slots from mainwindow.cpp and mainwindow.h
  2. Place a Push Button on the main window.
  3. Rename the push button to Read Radio Buttons.
  4. Go to the clicked slot for the push button.
  5. Add the code shown in the following listing to the slot for the push button clicked signal.

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    if (ui->rb1->isChecked()) {
        ui->lblChoice->setText("1%");
    }
    else if (ui->rb2->isChecked()) {
        ui->lblChoice->setText("2%");
    }
    else if (ui->rb5->isChecked()) {
        ui->lblChoice->setText("5%");
    }

}

3. Second Project to Find Selected Qt Radio Button

In this second project, radio buttons are tested in a group to see which one is checked.

Make a copy of the previous project, or simply modify it.

Delete the old code from the push button slot.

In Qt Creator, edit the window in design mode.

Group the radio buttons into a QButtonGroup by selecting all radio buttons.

Right-click the selected radio buttons and choose Assign to button group → New button group from the pop-up menu.

Modify the project code as follows.

mainwindow.cpp

#include <QString>
#include <QTextStream>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    int btn_id = 0;
    QString str;

    // Get the ID of the checked button
    // Defaults to -2, -3 and -4
    btn_id = ui->buttonGroup->checkedId();

    QTextStream(&str) << btn_id;

    ui->lblChoice->setText(str);
}

The above code displays the ID value from the QButtonGroup of the checked radio button. It returns a negative value starting from -2.

Modify the above code to update the label with text.

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    int btn_id = 0;

    btn_id = ui->buttonGroup->checkedId();

    switch (btn_id) {
        case -2:
            ui->lblChoice->setText("1%");
        break;

        case -3:
            ui->lblChoice->setText("2%");
        break;

        case -4:
            ui->lblChoice->setText("5%");
        break;
    }
}

Add code to manually change the radio button IDs in the QButtonGroup and set the desired ID values as shown below.

#include <QString>
#include <QTextStream>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Set preferred IDs of radio buttons in group
    ui->buttonGroup->setId(ui->rb1, 0);
    ui->buttonGroup->setId(ui->rb2, 1);
    ui->buttonGroup->setId(ui->rb5, 2);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    int btn_id = 0;
    QString str;

    // Get the ID of the checked button
    // Values are now 0, 1 and 2
    btn_id = ui->buttonGroup->checkedId();

    QTextStream(&str) << btn_id;

    ui->lblChoice->setText(str);
}

IDs of the three radio buttons are set to 0, 1 and 2 in the MainWindow constructor of the above code.

Leave a Reply

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