Button Widgets in Qt for C++ GUI Development

QPushButton

QPushButton is derived from QAbstractButton, an abstract base class for all button types.

Property Description
text The label displayed on the button.
icon An icon shown on the button.
iconSize Dimensions of the icon.
shortcut Keyboard shortcut to trigger the button.
autoRepeat If true, holding the mouse button triggers repeated click events (like auto-fire).
autoRepeatDelay Delay before auto-repeat starts when the button is held.
autoRepeatInterval Time between repeated triggers during auto-repeat.

Since QAbstractButton inherits from QWidget, all QWidget properties aply. The table lists QAbstractButton-specific attributes. Qt's API design allows getting and setting these properties via methods like text() and setText().

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    
    QIcon targetIcon(":/target.png");
    ui->btnTarget->setIcon(targetIcon);
    ui->btnTarget->setIconSize(QSize(80, 80));

    ui->btnMoveUp->setIcon(QIcon(":/up.png"));
    ui->btnMoveUp->setIconSize(QSize(50, 50));
    ui->btnMoveDown->setIcon(QIcon(":/down.png"));
    ui->btnMoveDown->setIconSize(QSize(50, 50));
    ui->btnMoveLeft->setIcon(QIcon(":/left.png"));
    ui->btnMoveLeft->setIconSize(QSize(50, 50));
    ui->btnMoveRight->setIcon(QIcon(":/right.png"));
    ui->btnMoveRight->setIconSize(QSize(50, 50));

    ui->btnMoveUp->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
    ui->btnMoveDown->setShortcut(QKeySequence(Qt::Key_S));
    ui->btnMoveLeft->setShortcut(QKeySequence(Qt::Key_A));
    ui->btnMoveRight->setShortcut(QKeySequence(Qt::Key_D));

    ui->btnMoveUp->setAutoRepeat(true);
    ui->btnMoveDown->setAutoRepeat(true);
    ui->btnMoveLeft->setAutoRepeat(true);
    ui->btnMoveRight->setAutoRepeat(true);
}

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

void MainWindow::on_btnMoveUp_clicked() {
    QRect pos = ui->btnTarget->geometry();
    ui->btnTarget->setGeometry(pos.x(), pos.y() - 5, pos.width(), pos.height());
}

void MainWindow::on_btnMoveDown_clicked() {
    QRect pos = ui->btnTarget->geometry();
    ui->btnTarget->setGeometry(pos.x(), pos.y() + 5, pos.width(), pos.height());
}

void MainWindow::on_btnMoveLeft_clicked() {
    QRect pos = ui->btnTarget->geometry();
    ui->btnTarget->setGeometry(pos.x() - 5, pos.y(), pos.width(), pos.height());
}

void MainWindow::on_btnMoveRight_clicked() {
    QRect pos = ui->btnTarget->geometry();
    ui->btnTarget->setGeometry(pos.x() + 5, pos.y(), pos.width(), pos.height());
}

QRadioButton

QRadioButton provides exclusive selection within a group of options.

Property Description
checkable Whether the button can be checked.
checked Indicates if the button is current checked (requires checkable).
autoExclusive If true, checking one button unchecks others in the same group (default for QRadioButton).
#include "selectiondialog.h"
#include "ui_selectiondialog.h"

SelectionDialog::SelectionDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SelectionDialog)
{
    ui->setupUi(this);
    ui->optMale->setChecked(true);
    ui->lblResult->setText("Selected gender: Male");
    
    ui->optOther->setEnabled(false);
}

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

void SelectionDialog::on_optMale_clicked() {
    ui->lblResult->setText("Selected gender: Male");
}

void SelectionDialog::on_optFemale_clicked() {
    ui->lblResult->setText("Selected gender: Female");
}

QButtonGroup manages exclusive groups of radio buttons.

OrderDialog::OrderDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::OrderDialog)
{
    ui->setupUi(this);
    
    QButtonGroup* beverageGroup = new QButtonGroup(this);
    QButtonGroup* mealGroup = new QButtonGroup(this);
    QButtonGroup* dessertGroup = new QButtonGroup(this);

    beverageGroup->addButton(ui->radCoffee);
    beverageGroup->addButton(ui->radTea);
    beverageGroup->addButton(ui->radJuice);

    mealGroup->addButton(ui->radBurger);
    mealGroup->addButton(ui->radPizza);
    mealGroup->addButton(ui->radSalad);

    dessertGroup->addButton(ui->radCake);
    dessertGroup->addButton(ui->radIceCream);
}

QCheckBox

QCheckBox allows multiple selections. Key properties checkable and checked are enherited from QAbstractButton. The tristate property enables a third indeterminate state.

#include "planner.h"
#include "ui_planner.h"

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

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

void Planner::on_btnShowPlan_clicked() {
    QString schedule = "Today's schedule: ";
    if (ui->chkExercise->isChecked()) {
        schedule += ui->chkExercise->text() + " ";
    }
    if (ui->chkRead->isChecked()) {
        schedule += ui->chkRead->text() + " ";
    }
    if (ui->chkWork->isChecked()) {
        schedule += ui->chkWork->text() + " ";
    }
    ui->lblSchedule->setText(schedule);
}

Tags: C++ Qt gui QPushButton QRadioButton

Posted on Wed, 27 May 2026 16:15:37 +0000 by RedMaster