In-depth exploration of the three states of QCheckBox and their usage

introduction:

QCheckBox is a commonly used check box control in the Qt framework, which provides three different states: unchecked, selected and partially selected. This article will introduce the meaning, usage and code examples of these three states in detail, to help readers better understand and apply the QCheckBox control.

three states

1. Unselected state (0):

The unchecked state is the default state of a QCheckBox, indicating that the option is not checked. In this state, the checkbox is hollow and its isChecked() method returns false. When the user clicks on the checkbox, the QCheckBox switches to the checked state.
Instructions:

// 创建一个QCheckBox对象
QCheckBox *checkBox = new QCheckBox("未选中状态", this);
// 设置默认状态为未选中
checkBox->setChecked(false);

2. Selected state (2):

The checked state indicates that the user has selected the option. In this state, the checkbox is filled and its isChecked() method returns true. When the user clicks on the checkbox, the QCheckBox switches to the unchecked state.

Instructions:

// 创建一个QCheckBox对象
QCheckBox *checkBox = new QCheckBox("选中状态", this);
// 设置默认状态为选中
checkBox->setChecked(true);

3. Partially selected state (1):

The partially selected state is a unique state of QCheckBox, which is very useful in some scenarios. It indicates that the status of the option is indeterminate, neither fully selected nor fully unselected. In this state, the check box is a solid rectangle, indicating that the state of the option is indeterminate. Its isChecked() method returns false.

Instructions:

// 创建一个QCheckBox对象
QCheckBox *checkBox = new QCheckBox("部分选中状态", this);
// 设置为部分选中状态
checkBox->setTristate(true);
checkBox->setCheckState(Qt::PartiallyChecked);

Judgment method

isChecked() judges two states and returns a boolean

checkState() can judge the three states and return the enumeration type enumQt::CheckState of the check box state, as follows:
insert image description here

in conclusion:

The three states of QCheckBox represent different states of options, and developers can use it flexibly according to actual needs. Through the introduction of this article, readers can better understand and apply the QCheckBox control, and flexibly use these states in their own projects.

Reference Code:

#include <QCheckBox>
#include <QVBoxLayout>
#include <QWidget>

int main(int argc, char *argv[]) {
    
    
    QApplication app(argc, argv);

    QWidget window;
    QVBoxLayout layout(&window);

    // 未选中状态
    QCheckBox *checkBox1 = new QCheckBox("未选中状态");
    checkBox1->setChecked(false);
    layout.addWidget(checkBox1);

    // 选中状态
    QCheckBox *checkBox2 = new QCheckBox("选中状态");
    checkBox2->setChecked(true);
    layout.addWidget(checkBox2);

    // 部分选中状态
    QCheckBox *checkBox3 = new QCheckBox("部分选中状态");
    checkBox3->setTristate(true);
    checkBox3->setCheckState(Qt::PartiallyChecked);
    layout.addWidget(checkBox3);

    window.show();
    return app.exec();
}

The above is the detailed introduction and usage of the three states of QCheckBox and their usage. I hope this article can help readers better understand and apply the QCheckBox control.

Guess you like

Origin blog.csdn.net/qq_46017342/article/details/132288340