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的使用的主要内容,如果未能解决你的问题,请参考以下文章