QCheckBox Qt Creator Tutorial

How to use a QCheckBox check box widget in a Qt Creator C++ program or application. This tutorial demonstrates the use of the QCheckBox class that implements a check box widget in Qt.

The example application built in this part of the Qt Creator tutorial series is shown in the image below. The result of clicking a check box is printed to stdout and can be seen in Application Output window of Qt Creator or at the command line.

Qt Creator QCheckBox Example Application
Qt Creator QCheckBox Example Application

Comparing Qt with GTK: The equivalent widget to a Qt QCheckBox in GTK is a GtkCheckButton. See the GtkCheckButton tutorial that uses a check box in a GTK C program for more information.

Part 19 of the Qt Creator C++ Tutorial

Go to the Qt Programming Tutorial Series Table of Contents

QCheckBox Tutorial Steps

Two examples are presented in the tutorial steps that follow. Both examples perform the same basic function, but do so in different ways. The first example uses three separate handler functions, or slots, for each check box. The second example uses a single slot for all three check boxes. Follow the tutorial steps below to build the two QCheckBox example programs.

1. Create a New Qt Widgets Application

Open Qt Creator and start a new Qt Widgets Application called checkbox. Open the mainwindow.ui form and delete the default widgets, as we have done in previous parts of this tutorial series.

2. Place the QCheckBox Widgets

Place three QCheckBox widgets in the main window as shown in the following image and described below.

Qt QCheckBox Widgets Placed in Window
Qt QCheckBox Widgets Placed in Window

In Qt Creator, find the Check Box widget under Buttons in the left widgets pane. Place three Check Box widgets on the main window by dragging and dropping them, one below the other.

Give each Check Box a objectName of chkbx_1, chkbx_2 and chkbx_3 from top to bottom.

Change the text of each Check Box to Option 1, Option 2 and Option 3 from top to bottom.

Group the three Check Box widgets together by dragging to select all of them. Now click the Lay Out Vertically icon on the top toolbar.

3. Edit the Main Window

Resize the main window to an appropriate size by first clicking the window to select it and then dragging the bottom right handle.

Change the windowTitle to Checkbox in the bottom right property pane under the QWidget section.

4. QCheckBox Example 1 C++ Code

Before writing the C++ code, add a slot for the clicked() signal of each check box widget, as described below.

4.1 Add a Slot for Each Check Box

While still editing mainwindow.ui in Design mode, right-click the top QCheckBox. On the menu that pops up, click Go to slot… In the dialog box that pops up, select the clicked() signal and then click the OK button.

Do the same for the other two check boxes. To get back to Design view, double-click mainwindow.ui in the left pane of Qt Creator, under the Forms item.

4.2 Write the C++ Code

After adding the last slot for the clicked() signal, modify mainwindow.cpp as follows.

mainwindow.cpp

#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_chkbx_1_clicked()
{
    QTextStream stream(stdout);

    if (ui->chkbx_1->isChecked()) {
        stream << "Option 1 is Checked" << endl;
    }
    else {
        stream << "Option 1 is Unchecked" << endl;
    }
}

void MainWindow::on_chkbx_2_clicked()
{
    QTextStream stream(stdout);

    if (ui->chkbx_2->isChecked()) {
        stream << "Option 2 is Checked" << endl;
    }
    else {
        stream << "Option 2 is Unchecked" << endl;
    }
}

void MainWindow::on_chkbx_3_clicked()
{
    QTextStream stream(stdout);

    if (ui->chkbx_3->isChecked()) {
        stream << "Option 3 is Checked" << endl;
    }
    else {
        stream << "Option 3 is Unchecked" << endl;
    }
}

4.3 Build and Run Example 1

Build example 1 and run it from within Qt Creator. When any of the check boxes are clicked in the application, text is written to the command line which is captured in Qt Creator. A pane at the bottom of Qt Creator, called Application Output opens and displays the text that the application prints.

The image below shows the bottom pane from Qt Creator. It contains text printed by the application from check box clicks.

Output from QCheckBox Example 1 in Qt Creator
Output from QCheckBox Example 1 in Qt Creator

4.4 How the Program Works

The program is very simple and uses a QTextStream to send text to stdout to display the status of a check box. Part 7 of this tutorial series, Qt Console Application Programming covers using QTextStream.

Whenever a QCheckBox is clicked in the application, the isChecked() method is called to check whether the checkbox is checked or unchecked. The result is printed to stdout, which is captured by Qt Creator when the application is run from Qt Creator.

5. QCheckBox Example 2 C++ Code

In this second example that uses QCheckBox, a single slot handles all three check boxes. Code manually connects signals for each checkbox to a single slot.

5.1 Create a New Qt Widgets Application

Create a new Qt widgets application exactly as done in section 1 of this tutorial. This time call it checkbox2.

5.2 Write the C++ Code

In this example, we add the slot manually in the C code. Open mainwindow.h and add the slot as shown in the code listing that follows.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    void onchkbx_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

Now open mainwindow.cpp and modify the code as follows.

mainwindow.cpp

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

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

    connect(ui->chkbx_1, SIGNAL(clicked(bool)), this, SLOT(onchkbx_clicked()));
    connect(ui->chkbx_2, SIGNAL(clicked(bool)), this, SLOT(onchkbx_clicked()));
    connect(ui->chkbx_3, SIGNAL(clicked(bool)), this, SLOT(onchkbx_clicked()));
}

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

void MainWindow::onchkbx_clicked()
{
    QTextStream stream(stdout);

    if (ui->chkbx_1->isChecked()) {
        stream << "Option 1 is Checked" << endl;
    }
    else {
        stream << "Option 1 is Unchecked" << endl;
    }

    if (ui->chkbx_2->isChecked()) {
        stream << "Option 2 is Checked" << endl;
    }
    else {
        stream << "Option 2 is Unchecked" << endl;
    }

    if (ui->chkbx_3->isChecked()) {
        stream << "Option 3 is Checked" << endl;
    }
    else {
        stream << "Option 3 is Unchecked" << endl;
    }
}

5.3 Build and Run Example 2

Save the modifications made to the code and form. Build the application in Qt Creator and run it.

Click a check box and see the status of all three check boxes displayed in the bottom pane of Qt Creator.

5.4 How the Code Works

A slot is manually added in mainwindow.h as follows.

private slots:

    void onchkbx_clicked();

The body of the onchkbx_clicked() slot is then added to mainwindow.cpp. In this slot, the state of each checkbox is checked using its isChecked() method. The QTextStream object writes check box states to stdout.

In the constructor, connect() connects the clicked() signal of each checkbox to the same onchkbx_clicked() slot.

The Signals and Slots Qt documentation page contains information on the Qt signal slot mechanism.

connect() is a member function of QObject class. Read more about QObject::connect in the Qt QObject class documentation.

Leave a Reply

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