Qt4 QRadioButton和QCheckBox用法示例

初学Qt,简单示例QRadioButton和QCheckBox的用法。

mybuttonwindow.h

#ifndef MYBUTTONWINDOW_H
#define MYBUTTONWINDOW_H
#include <QWidget>
#include <QPushButton>
#include <QGroupBox>
#include <QRadioButton>
#include <QCheckBox>
#include <QLabel>
class MyButtonWindow : public QWidget
{
    Q_OBJECT
public:
    explicit MyButtonWindow(QWidget *parent = 0);
    void initButtons();
    
signals:
    
public slots:
    void radioChange();
    void checkChange();
    
private:
    QGroupBox *radioGroup;
    QRadioButton *radio1;
    QRadioButton *radio2;
    QRadioButton *radio3;
    QLabel *label11;
    QLabel *label12;
    QLabel *label13;
    QLabel *label1;
    QGroupBox *checkGroup;
    QCheckBox *check1;
    QCheckBox *check2;
    QCheckBox *check3;
    QLabel *label21;
    QLabel *label22;
    QLabel *label23;
    QLabel *label2;
};
#endif // MYBUTTONWINDOW_H


 mybuttonwindow.cpp 

#include "mybuttonwindow.h"
#include <QPixmap>
#include <QBitmap>

MyButtonWindow::MyButtonWindow(QWidget *parent) :
    QWidget(parent)
{
    setGeometry(100,100,230,190);

    // radio group
    radioGroup = new QGroupBox("Options 1", this);
    radioGroup->setGeometry(10,10,100,110);

    radio1 = new QRadioButton("Choice1", this);
    radio1->move(20,35);
    radio2 = new QRadioButton("Choice2", this);
    radio2->move(20,60);
    radio3 = new QRadioButton("Choice3", this);
    radio3->move(20,85);

    label1 = new QLabel("Option1 Result:", this);
    label1->setGeometry(10,130,100,20);
    label1->setAlignment(Qt::AlignRight);

    // check group
    checkGroup = new QGroupBox("Options 2", this);
    checkGroup->setGeometry(120,10,100,110);

    check1 = new QCheckBox("Choice1", this);
    check1->move(130,35);
    check2 = new QCheckBox("Choice2", this);
    check2->move(130,60);
    check3 = new QCheckBox("Choice3", this);
    check3->move(130,85);

    label2 = new QLabel("Option2 Result:", this);
    label2->setGeometry(10,160,100,20);
    label2->setAlignment(Qt::AlignRight);

    // init buttons
    initButtons();

    // signals and slots
    connect(radio1, SIGNAL(clicked(bool)), this, SLOT(radioChange()));
    connect(radio2, SIGNAL(clicked(bool)), this, SLOT(radioChange()));
    connect(radio3, SIGNAL(clicked(bool)), this, SLOT(radioChange()));
    connect(check1, SIGNAL(clicked()), this, SLOT(checkChange()));
    connect(check2, SIGNAL(clicked()), this, SLOT(checkChange()));
    connect(check3, SIGNAL(clicked()), this, SLOT(checkChange()));
}

void MyButtonWindow::initButtons()
{
    QPixmap pixmap1(":/btn1_gray");
    QPixmap pixmap2(":/btn2_gray");
    QPixmap pixmap3(":/btn3_gray");

    label11 = new QLabel("", this);
    label11->setGeometry(120,129,18,19);
    label11->setPixmap(pixmap1);
    label11->resize(pixmap1.size());

    label12 = new QLabel("", this);
    label12->setGeometry(149,129,18,19);
    label12->setPixmap(pixmap2);
    label12->resize(pixmap2.size());

    label13 = new QLabel("", this);
    label13->setGeometry(178,129,18,19);
    label13->setPixmap(pixmap3);
    label13->resize(pixmap3.size());

    label21 = new QLabel("", this);
    label21->setGeometry(120,159,18,19);
    label21->setPixmap(pixmap1);
    label21->resize(pixmap1.size());

    label22 = new QLabel("", this);
    label22->setGeometry(149,159,18,19);
    label22->setPixmap(pixmap2);
    label22->resize(pixmap2.size());

    label23 = new QLabel("", this);
    label23->setGeometry(178,159,18,19);
    label23->setPixmap(pixmap3);
    label23->resize(pixmap3.size());
}

void MyButtonWindow::radioChange()
{
    QPixmap pixmap1(":/btn1_gray");
    QPixmap pixmap2(":/btn2_gray");
    QPixmap pixmap3(":/btn3_gray");
    label11->setPixmap(pixmap1);
    label11->resize(pixmap1.size());
    label12->setPixmap(pixmap2);
    label12->resize(pixmap2.size());
    label13->setPixmap(pixmap3);
    label13->resize(pixmap3.size());

    QString url;
    QLabel *label;
    if (sender() == radio1)
    {
        url = ":/btn1_bright";
        label = label11;
    }
    else if (sender() == radio2)
    {
        url = ":/btn2_bright";
        label = label12;
    }
    else if (sender() == radio3)
    {
        url = ":/btn3_bright";
        label = label13;
    }

    QPixmap pixmap(url);
    label->setPixmap(pixmap);
    label->resize(pixmap.size());
}

void MyButtonWindow::checkChange()
{
    QString url;
    QLabel *label;
    if (sender() == check1)
    {
        url = check1->isChecked() ? ":/btn1_bright" : ":/btn1_gray";
        label = label21;
    }
    else if (sender() == check2)
    {
        url = check2->isChecked() ? ":/btn2_bright" : ":/btn2_gray";
        label = label22;
    }
    else if (sender() == check3)
    {
        url = check3->isChecked() ? ":/btn3_bright" : ":/btn3_gray";
        label = label23;
    }

    QPixmap pixmap(url);
    label->setPixmap(pixmap);
    label->resize(pixmap.size());
}
main.cpp

#include <QApplication>
#include <QTextCodec>
#include "mybuttonwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("System"));  //消除乱码

    MyButtonWindow buttonWindow;
    buttonWindow.show();

    return a.exec();
}
运行结果示例:


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Qt 是一种跨平台的 C++ 应用程序框架,它可以轻松地构建图形用户界面、网络应用程序以及嵌入式应用程序等。Qt5 作为 Qt 框架的最新版本,提供了更加丰富的功能和性能优化,同时还规范了框架的编程接口和命名规则,使得应用程序的开发变得更加便捷。 Qt5 的中文文档是 Qt 官方提供的一份技术文档,对于 Qt 开发者来说,它是一份重要的参考资料。这份中文文档将 Qt5 的各个模块、类、函数以及代码示例详细地介绍了一番,使得从初学者到高级开发者都可以轻松地使用 Qt5。具体来说,这份文档主要包含以下内容: 1. Qt5 的开发环境搭建与使用方法。包含了 Qt5 的安装、配置以及常见问题的解决办法。对于初学者来说,这部分文档非常重要。 2. Qt5 的基础概念。涵盖了 Qt5 的基本概念,例如 QWidget、Layout、Signal & Slot 等等。 3. Qt5 常用控件的使用。如 QPushButton、QLabel、QLineEdit,QComboBox、QCheckBox、QRadioButton,QTextEdit、QSlider、QSpinBox 等等。这些控件是 Qt5 编程中常用的控件。 4. Qt5 的高级模块 功能。如 Qt5 的网络模块、数据库模块、XML 模块、多媒体模块等等,了解这些模块可以让我们更加深入地理解 Qt5的编程思想。同时这也是开发复杂应用程序的关键。 5. Qt5 中的线程和并发编程。Qt5 在并发编程上有很好的支持,开发者可以通过 QThread、QMutex、QSemaphore 等控件来进行线程编程。 需要注意的是,由于 Qt 是一种跨平台的应用程序框架,因此 Qt5 文档同样支持多种语言,如英语、中文、日语、西班牙语、葡萄牙语、德语、法语等等。在日常开发中,可以根据自己的需求选择不同语言的文档来参考。 总的来说,Qt5 的中文文档是 Qt 开发者必备的参考资料。它包含了 Qt5 的各个方面,涵盖了开发的各个阶段。开发者只要熟练掌握这份文档的内容,就能够快速地开发出高质量的 Qt5 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值