常用控件QCheckBox
- QCheckBox表示复选按钮,可以允许选中多个。
QCheckBox继承自QAbstractButton
例子:获取复选按钮的取值
使用Qt Designer先大体进行设计
代码实现:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QString result = "今天需要做的事情:";
if(ui->checkBox_eat->isChecked())
{
result += ui->checkBox_eat->text() + " ";
}
if(ui->checkBox_sleep->isChecked())
{
result += ui->checkBox_sleep->text() + " ";
}
if(ui->checkBox_play->isChecked())
{
result += ui->checkBox_play->text() + " ";
}
ui->label->setText(result);
}