Qt基础 07_单选框、复选框及实例

来源:互联网 发布:java 写一句话木马 编辑:程序博客网 时间:2024/06/06 03:04

/******************************************************
* 课程名 :QT编程
* 时 间 :2017年6月5日(周一)下午前半段
* 工程名 :09-checkBox
* 类 名 :dialog09
* 内 容 :单选框、复选框
* CSDN :Qt基础 07_单选框、复选框及实例
******************************************************/

目标:建立单选框、复选框,并将用户的选择显示在窗口中

一、绘制ui界面

通过拖拽GroupBox、CheckBox、RadioButton、Label等部件,完成如下界面:
绘制ui界面
GroupBox的名称变为:hoobbyBox和cityBox
CheckBox的名称变为:runCheckBox、swimCheckBox和ballCheckBox
RadioButton的名称变为:jxRadioButton、jsRadioButton和zjRadioButton
label的名称变为:hoobbylabel和citylabel

二、使用户点击的复选框呈现在窗口中

步骤一:在dialog09.h中定义槽函数:

private slots:    void showHobbyToLabel( );

在dialog09.cpp中实现

void Dialog09::showToLabel( ){    QString str = "";    if(ui->runCheckBox->isChecked())    {        str += ui->runCheckBox->text() + " ";    }    if(ui->swimCheckBox->isChecked())    {        str += ui->swimCheckBox->text() + " ";    }    if(ui->ballCheckBox->isChecked())    {        str += ui->ballCheckBox->text() + " ";    }    //显示    ui->hoobbylabel->setText(str);}

步骤二:在构造函数中连接

Dialog09::Dialog09(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog09){    ui->setupUi(this);    connect(ui->runCheckBox,SIGNAL(stateChanged(int)),this,SLOT(showHobbyToLabel()));    connect(ui->swimCheckBox,SIGNAL(clicked()),this,SLOT(showHobbyToLabel()));    connect(ui->ballCheckBox,SIGNAL(clicked()),this,SLOT(showHobbyToLabel()));}

三、使用户点击的单选框呈现在窗口中

步骤一:在dialog09.h中定义槽函数:

private slots:    void showCityToLabel( );

在dialog09.cpp中实现

void Dialog09::showCityToLabel(){    QString str = "";    if(ui->jxRadioButton->isChecked())    {        str += ui->jxRadioButton->text() + " ";    }    if(ui->jsRadioButton->isChecked())    {        str += ui->jsRadioButton->text() + " ";    }    if(ui->zjRadioButton->isChecked())    {        str += ui->zjRadioButton->text() + " ";    }    //显示    ui->citylabel->setText(str);}

步骤二:在构造函数中连接:

    //初始化子部件    ui->zjRadioButton->setChecked(true);    //代码——使"浙江"变为默认    //连接。单选框    connect(ui->jxRadioButton,SIGNAL(stateChanged(int)),this,SLOT(showCityToLabel()));    connect(ui->jsRadioButton,SIGNAL(clicked()),this,SLOT(showCityToLabel()));    connect(ui->zjRadioButton,SIGNAL(clicked()),this,SLOT(showCityToLabel()));

完成。
源代码:http://download.csdn.net/download/c_estbon/9865381

原创粉丝点击