初学Qt,简单示例QRadioButton和QCheckBox的用法。
mybuttonwindow.h
mybuttonwindow.cpp#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
#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(); }运行结果示例: