Qt5 学习之路及嵌入式开发教程8:单选按钮、复选框

Qt5 学习之路及嵌入式开发教程8:单选按钮、复选框

第一部分:有界面设计

一、按钮组(Buttons)之Radio Button,Check Button:单选按钮,复选框

1、Qt GUI中,单选框类是QRadioButton,复选框类是QCheckBox。它们都是QAbstractButton的派生类。

2、编程示例:小试牛刀

(1)、建立应用程序

选择后,命名、两个下一步后进入设置界面

下一步后点击完成。

⑵、进入设置界面,进行界面设计

①、拖曳Radio Button控件,两个Label控件到面板,

②、对控件进行属性设置

Radio Button文本改为“单选按钮示例”

 

第一个Label文本改为:你的选择答案:

第二个Label文本删除。用来显示单选的内容。

整体设计完成

运行结果图:

(3)、添加代码,完成具体功能

①、右击“单选按钮示例”,找到“转到槽”,进入编辑代码

②、运行结果

3、小功告成:多个单选按钮组的设计与编程

(1)、按上例过程重新建立工程项目文件和设置多个RadioButton按钮组(按钮组采用GroupBox形式)

设置属性:

GroupBox名称

文本:

(2)在GroupBox设置多个RadioButton

对应的属性分别为:

(3)、设置两个Label控件,其中一个用来显示选择的水果。

整体设置效果为:

(4)、添加代码,完成单选按钮功能

在dialog.h中添加槽函数:

在dialog.cpp中添加信号槽:

然后,编写实现信号槽功能。

(5)、运行,查看效果

   

小功告成。

4、复选框的设计

(1)、拖曳一个GroupBox控件到右侧,框内设置4个复选框,并再添加两个Label控件

整个设置界面为:

对应属性:

足球:

篮球:

排球:

网球:

标签属性:

(2)、添加代码,实现复选框功能

在dialog.h中添加槽函数:

在dialog.cpp中添加信号槽:

先导入头文件:

然后添加信号槽:

最后,实现信号槽功能。(这段代码要好好理解)

//复选框按钮功能实现
QString str;
void Dialog::checkChange()
{
        if (sender() == ui->FootballCheckBox)
        {
            if (ui->FootballCheckBox->checkState() == Qt::Checked)
            {
                str += "足球";
            }
            else
            {
                str = str.replace(QString("足球"), QString(""));
            }

        }

        else if (sender() == ui->BasketBallCheckBox)
        {
            if (ui->BasketBallCheckBox->checkState() == Qt::Checked)
            {
                str += " 篮球";
            }
            else
            {
                str = str.replace(QString("篮球"), QString(""));
            }

        }

        else if (sender() == ui->VolleyballcheckBox)
        {
            if (ui->VolleyballcheckBox->checkState() == Qt::Checked)
            {
                str += " 排球";
            }
            else
            {
                str = str.replace(QString("排球"), QString(""));
            }

        }

        else if (sender() == ui->TennisCheckBox)
        {
            if (ui->TennisCheckBox->checkState() == Qt::Checked)
            {
                str += " 网球";
            }
            else
            {
                str = str.replace(QString("网球"), QString(""));
            }

        }

        ui->label_4->setText(str);
}

(3)运行程序,查看结果

大功告成!!!

第二部分:用代码书写

1、创建工程项目文件

把创建界面的“勾”取消,下一步后点击完成

2、添加代码,先实现单选按钮功能

(1)、在dialog.h文件中添加头文件

然后声明变量,四个单选按钮变量,两个标签变量

最后添加信号槽函数:

(2)、在dialog.cpp文件中,实现变量初始化,大小,动作功能等。

①、//设置窗体大小

②、变量初始化

③、//设置控件位置

④、布局

⑤、每个控件内的内容

⑥、在窗体构造函数中初始化QButtonGroup,把相应的QRadioButton添加进来并设置ID

⑦、在窗体构造函数中绑定信号和槽函数

⑧、完成信号槽函数

(3)、运行程序,查看结果

源代码:

//dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

#include <QButtonGroup>
#include <QRadioButton>
#include <QLabel>
#include <QGridLayout>
#include <QGroupBox>
#include <QVBoxLayout>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QVBoxLayout     *vbox;                  //QVBoxLayout类垂直地摆放小部件
    QGroupBox       *radioGroupBox;         //QGroupBox控件
    QGridLayout     *mainLayout;            //QGridLayout布局
    QButtonGroup    *RadioButtonGroup;      //声明单选按钮组合框

    //声明香蕉、苹果、桔子、葡萄4个单选按钮变量
    QRadioButton    *BananaRadioButton;
    QRadioButton    *AppleRadioButton;
    QRadioButton    *OrangeRadioButton;
    QRadioButton    *GrapeRadioButton;

    //标签变量,第二个用来显示单选结果
    QLabel          *fruits_1;
    QLabel          *fruits_2;

// 用来实现单选按钮的选择动作
private slots:
    void    slots_fruits();

};

#endif // DIALOG_H



//dialog.cpp
#include "dialog.h"


Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{

    //设置窗体大小
    setMaximumSize(500,300);
    setMinimumSize(500,300);

    //变量初始化
    vbox                = new QVBoxLayout(this);
    radioGroupBox       = new QGroupBox(this);
    mainLayout          = new QGridLayout(this);
    RadioButtonGroup    = new QButtonGroup(this);

    BananaRadioButton   = new QRadioButton(this);
    AppleRadioButton    = new QRadioButton(this);
    OrangeRadioButton   = new QRadioButton(this);
    GrapeRadioButton    = new QRadioButton(this);

    fruits_1            = new QLabel(this);
    fruits_2            = new QLabel(this);


    //设置控件位置
    radioGroupBox->setGeometry(40,40,100,100);
    fruits_1->setGeometry(40,140,100,30);
    fruits_2->setGeometry(140,140,240,30);


    //布局GroupBox
    mainLayout->addWidget(radioGroupBox);

    //布局单选按钮
    vbox->addWidget(BananaRadioButton);
    vbox->addWidget(AppleRadioButton);
    vbox->addWidget(OrangeRadioButton);
    vbox->addWidget(GrapeRadioButton);

    //GroupBox中布局单选按钮
    radioGroupBox->setLayout(vbox);

    //GroupBox标题
    radioGroupBox->setTitle("单选按钮示例");

    //单选按钮变量的内容
    BananaRadioButton->setText("香蕉");
    AppleRadioButton->setText("苹果");
    OrangeRadioButton->setText("桔子");
    GrapeRadioButton->setText("葡萄");

    //第一个标签变量的内容
    fruits_1->setText("您选择的水果是:");

    //在窗体构造函数中初始化QButtonGroup,把相应的QRadioButton添加进来并设置ID
    RadioButtonGroup->addButton(BananaRadioButton,0);
    RadioButtonGroup->addButton(AppleRadioButton, 1);
    RadioButtonGroup->addButton(OrangeRadioButton,2);
    RadioButtonGroup->addButton(GrapeRadioButton, 3);


    //在窗体构造函数中绑定信号和槽函数
    connect(BananaRadioButton,SIGNAL(clicked(bool)),this,SLOT(slots_fruits()));
    connect(AppleRadioButton, SIGNAL(clicked(bool)),this,SLOT(slots_fruits()));
    connect(OrangeRadioButton,SIGNAL(clicked(bool)),this,SLOT(slots_fruits()));
    connect(GrapeRadioButton, SIGNAL(clicked(bool)),this,SLOT(slots_fruits()));

}

Dialog::~Dialog()
{

}


void Dialog::slots_fruits()
{
    switch(RadioButtonGroup->checkedId())
   {
    case 0:
        fruits_2->setText("香蕉");
        break;

    case 1:
        fruits_2->setText("苹果");
        break;

    case 2:
        fruits_2->setText("桔子");
        break;

    case 3:
        fruits_2->setText("葡萄");
        break;
   }
}


//main.cpp
#include "dialog.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;

    w.setWindowTitle(QString("单选按钮示例"));

    w.show();

    return a.exec();
}

 

发布了39 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/fjqlldg/article/details/105107263
今日推荐