Qt Learning - QCheckBox Checkbox

Zero. Introduction

QCheckBox is a check box, which is often used for non-mutually exclusive selection of forms, and is used in the scenario of "multiple selection and multiple".

Checkboxes are used in both office word and PhotoShop, as follows:

1. Create a QCheckBox

QCheckBox has two constructors, both of which specify the parent object, one of which can set the text of the checkbox.

QCheckBox(QWidget *parent = nullptr);
QCheckBox(const QString &text, QWidget *parent = nullptr);

It can also be created by dragging the control, or it can be created directly by code. By default, the control creation uses the constructor QCheckBox(QWidget *parent = nullptr);.

Three radio buttons are created as shown in the figure below. Among them, CheckBox is created by dragging and dropping controls, and CheckBox1 and CheckBox2 are created directly by code:

2. Member functions and signals

In addition to the signal inherited from QAbstractButton, QCheckBox also has its own signal void stateChanged(int);, when the state of the checkbox changes, the signal will be emitted and the state of the checkbox will be returned. There are three states of the checkbox. :

ConstantValueDescription
Qt::Unchecked0not selected
Qt::PartiallyChecked1Partially checked, i.e. the checkbox has children and the children are not all checked
Qt::Checked2chosen

In addition to inheriting from QAbstractButton, the member functions of QCheckBox have two more commonly used ones.

function prototypedescribe
void setTristate(bool y = true);Set whether the checkbox can be in three states, the function parameter defaults to true, but the property defaults to false
bool isTristate() const;Get whether the checkbox can be in three states
void setCheckState(Qt::CheckState state);set checkbox state
Qt::CheckState checkState() const;Get checkbox state

3. Examples

1. Set whether the checkbox can be in three states

checkBox2->setTristate(true);

As shown in the figure, the three states of CheckBox are shown respectively.

2. Set the checkbox state

In addition to changing the checkbox state by mouse click, you can also set it by code:

where checkbox states are listed by enumeration:

enum CheckState {
    Unchecked,
    PartiallyChecked,
    Checked
};

3. Take corresponding actions through the checkbox state

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

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

    QCheckBox* checkBox1 = new QCheckBox("CheckBox1", this);
    checkBox1->setGeometry(QRect(130, 160, 111, 22));

    QCheckBox* checkBox2 = new QCheckBox("CheckBox2", this);
    checkBox2->setGeometry(QRect(130, 190, 111, 22));
    checkBox2->setTristate(true);
    checkBox2->setCheckState(Qt::CheckState::Checked);

    connect(checkBox2, SIGNAL(stateChanged(int)),
            this, SLOT(checkBox2CallBack(int)));
}

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

void Widget::checkBox2CallBack(int state)
{
    switch(state)
    {
    case Qt::CheckState::Checked:
        qDebug("checkBox2 is checked.");
        break;
    case Qt::CheckState::PartiallyChecked:
        qDebug("checkBox2 is partiallyChecked.");
        break;
    case Qt::CheckState::Unchecked:
        qDebug("checkBox2 is unchecked.");
        break;
    default:
        break;
    }
}

The print result is as follows:

4. Actions through multiple checkbox states

Here you can use the button group (QButtonGroup), the code is as follows:

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QButtonGroup>

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

    QCheckBox* checkBox1 = new QCheckBox("CheckBox1", this);
    checkBox1->setGeometry(QRect(130, 160, 111, 22));

    QCheckBox* checkBox2 = new QCheckBox("CheckBox2", this);
    checkBox2->setGeometry(QRect(130, 190, 111, 22));

    m_btnGroup = new QButtonGroup(this);
    m_btnGroup->addButton(ui->checkBox, 0);
    m_btnGroup->addButton(checkBox1, 1);
    m_btnGroup->addButton(checkBox2, 2);

    connect(m_btnGroup, SIGNAL(idClicked(int)),
            this, SLOT(btnGroupCallBack(int)));
}

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

void Widget::btnGroupCallBack(int btn)
{
    if (m_btnGroup->button(btn)->isChecked())
    {
        switch(btn)
        {
        case 0:
            qDebug("checkBox0 is checked.");
            break;
        case 1:
            qDebug("checkBox1 is checked.");
            break;
        case 2:
            qDebug("checkBox2 is checked.");
            break;
        default:
            break;
        }
    }
}

The print result is as follows:

Tags: Qt

Posted by magic-eyes on Sun, 06 Nov 2022 04:12:17 +0300