Qt CheckBox的使用

Posted 别呀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt CheckBox的使用相关的知识,希望对你有一定的参考价值。

方法一

1、首先拖动一个checkBox按钮放在界面上,然后就是需要,选择转到槽中的槽函数,如下,选择这个:

2、代码部分:

void MyWidget::on_checkBox_stateChanged(int arg1)

    //第一种
    if(arg1 == 2)
    
        qDebug()<<"表示被选中1";
    
    else if(arg1 == 0)
    
        qDebug()<<"表示未被选中";
    

    //第二种
    bool status =ui->checkBox->isChecked();
    qDebug()<<"status"<<status;
    if(status == true)
    
        qDebug()<<"表示被选中2";

    
    else if(status == false)
    
        qDebug()<<"表示未被选中2";
    

结果如下:

//第一种
表示被选中1 
表示未被选中 

//第二种
status true 
表示被选中2 
status false 
表示未被选中2

方法二

1、首先拖动一个checkBox按钮放在界面上,然后就是需要,选择转到槽中的槽函数,如下,选择这个:

2、代码部分:

在构造函数中添加:

ui->checkBox_2->setCheckState(Qt::Unchecked);  //设置初始状态
checkBox_state = Qt::Checked;  //checkBox_state 是在类中声明的,类型为int型

在槽函数中

void MyWidget::on_checkBox_2_clicked()

    if (checkBox_state == Qt::Checked) // "选中"
    
        qDebug()<<"表示被选中1";
        checkBox_state = Qt::Unchecked;
    
    else if(checkBox_state == Qt::Unchecked)// 未选中
    
        qDebug()<<"表示未被选中";
        checkBox_state = Qt::Checked;
    

结果如下:

表示被选中1 
表示未被选中 

以上是关于Qt CheckBox的使用的主要内容,如果未能解决你的问题,请参考以下文章

QT 进行批量创建checkBox的方法

Qt CheckBox 绑定到属性

QT软件开发之基础控件--2.1.4 checkBox按钮

Qt的checkbox风格修改

qt4.8.3中checkbox的大小如何改变?不是整个checkbox的尺寸,是中间那个打钩的小方块的尺寸。

Qt中QListView怎么在每行嵌入CheckBox以及edit