QT常用控件——QCheckBox输入控件

操作系统: 统信UOSv20
Qt版本: 5.11.3

一,简单介绍

QCheckBox是一个选项按钮,可以打开(选中)或关闭(未选中)。复选框通常用于表示应用程序中可以启用或禁用而不影响其他功能的功能。可以实现不同类型的行为。提供了多选多的选择模式

项目 内容
Header QCheckBox
qmake QT += widgets
Inherits QAbstractButton

二,常用方法和信号

1. 常用方法

方法 描述
setText(const QString &text) 设置显示文本
setChecked(bool) 未开启三态模式设置选中状态,默认为未选中,true为选中
isChecked() 判断是否选中
setTristate(bool y = true) true为开启三态模式
setCheckState(Qt::CheckState state) 开启三态模式后设置选中状态

2.Qt::CheckState类型

类型 描述
Qt::Unchecked 0 未选中
Qt::PartiallyChecked 1 半选中
Qt::Checked 2 选中

3. 常用信号

信号 描述
stateChanged(int) 选中状态改变触发该信号

三,使用介绍

1. 创建对象及基本设置

QCheckBox默认是两种选择状态,选中和未选中,可以使用setChecked()来设置选择状态。setTristate(true)用来开启三态选择模式,开启三态模式后通过

QCheckBox *checkBox1 = new QCheckBox(this);             //创建QCheckBox对象
checkBox1->move(100, 50);                               //设置位置
checkBox1->setText("语文");                              //设置显示文本

QCheckBox *checkBox2 = new QCheckBox(this);
checkBox2->move(100, 100);
checkBox2->setText("数学");
checkBox2->setChecked(true);                            //设置选中状态,默认为未选中

QCheckBox *checkBox3 = new QCheckBox(this);
checkBox3->move(100, 150);
checkBox3->setText("英语");

checkBox3->setTristate(true);                           //开启三态选择模式
checkBox3->setCheckState(Qt::PartiallyChecked);         //设置选中状态

在这里插入图片描述

2. 通过信号判断选中状态

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
    
    
class Widget;
}

class Widget : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void slotCheckedChanged(int state);			//声明槽函数

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QCheckBox>

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

    QCheckBox *checkBox1 = new QCheckBox(this);             //创建QCheckBox对象
    checkBox1->move(100, 50);                               //设置位置
    checkBox1->setText("语文");                              //设置显示文本

    QCheckBox *checkBox2 = new QCheckBox(this);
    checkBox2->move(100, 100);
    checkBox2->setText("数学");
    checkBox2->setChecked(true);                            //设置选中状态,默认为未选中

    QCheckBox *checkBox3 = new QCheckBox(this);
    checkBox3->move(100, 150);
    checkBox3->setText("英语");

    checkBox3->setTristate(true);                           //开启三态选择模式
    checkBox3->setCheckState(Qt::PartiallyChecked);         //设置选中状态

    connect(checkBox3, SIGNAL(stateChanged(int)),
            this, SLOT(slotCheckedChanged(int)));
}

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

void Widget::slotCheckedChanged(int state)
{
    
    
    if (state == Qt::Checked)
    {
    
    
        qDebug() << "选中";
    }
    else if (state == Qt::Unchecked)
    {
    
    
        qDebug() << "未选中";
    }
    else if (state == Qt::PartiallyChecked)
    {
    
    
        qDebug() << "半选中";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43657810/article/details/118173951