初学Qt,简单示例QRadioButton和QCheckBox的用法。
mybuttonwindow.h
1 #ifndef MYBUTTONWINDOW_H 2 #define MYBUTTONWINDOW_H 3 #include <QWidget> 4 #include <QPushButton> 5 #include <QGroupBox> 6 #include <QRadioButton> 7 #include <QCheckBox> 8 #include <QLabel> 9 class MyButtonWindow : public QWidget 10 { 11 Q_OBJECT 12 public: 13 explicit MyButtonWindow(QWidget *parent = 0); 14 void initButtons(); 15 16 signals: 17 18 public slots: 19 void radioChange(); 20 void checkChange(); 21 22 private: 23 QGroupBox *radioGroup; 24 QRadioButton *radio1; 25 QRadioButton *radio2; 26 QRadioButton *radio3; 27 QLabel *label11; 28 QLabel *label12; 29 QLabel *label13; 30 QLabel *label1; 31 QGroupBox *checkGroup; 32 QCheckBox *check1; 33 QCheckBox *check2; 34 QCheckBox *check3; 35 QLabel *label21; 36 QLabel *label22; 37 QLabel *label23; 38 QLabel *label2; 39 }; 40 #endif // MYBUTTONWINDOW_H
mybuttonwindow.cpp
1 #include "mybuttonwindow.h" 2 #include <QPixmap> 3 #include <QBitmap> 4 5 MyButtonWindow::MyButtonWindow(QWidget *parent) : 6 QWidget(parent) 7 { 8 setGeometry(100,100,230,190); 9 10 // radio group 11 radioGroup = new QGroupBox("Options 1", this); 12 radioGroup->setGeometry(10,10,100,110); 13 14 radio1 = new QRadioButton("Choice1", this); 15 radio1->move(20,35); 16 radio2 = new QRadioButton("Choice2", this); 17 radio2->move(20,60); 18 radio3 = new QRadioButton("Choice3", this); 19 radio3->move(20,85); 20 21 label1 = new QLabel("Option1 Result:", this); 22 label1->setGeometry(10,130,100,20); 23 label1->setAlignment(Qt::AlignRight); 24 25 // check group 26 checkGroup = new QGroupBox("Options 2", this); 27 checkGroup->setGeometry(120,10,100,110); 28 29 check1 = new QCheckBox("Choice1", this); 30 check1->move(130,35); 31 check2 = new QCheckBox("Choice2", this); 32 check2->move(130,60); 33 check3 = new QCheckBox("Choice3", this); 34 check3->move(130,85); 35 36 label2 = new QLabel("Option2 Result:", this); 37 label2->setGeometry(10,160,100,20); 38 label2->setAlignment(Qt::AlignRight); 39 40 // init buttons 41 initButtons(); 42 43 // signals and slots 44 connect(radio1, SIGNAL(clicked(bool)), this, SLOT(radioChange())); 45 connect(radio2, SIGNAL(clicked(bool)), this, SLOT(radioChange())); 46 connect(radio3, SIGNAL(clicked(bool)), this, SLOT(radioChange())); 47 connect(check1, SIGNAL(clicked()), this, SLOT(checkChange())); 48 connect(check2, SIGNAL(clicked()), this, SLOT(checkChange())); 49 connect(check3, SIGNAL(clicked()), this, SLOT(checkChange())); 50 } 51 52 void MyButtonWindow::initButtons() 53 { 54 QPixmap pixmap1(":/btn1_gray"); 55 QPixmap pixmap2(":/btn2_gray"); 56 QPixmap pixmap3(":/btn3_gray"); 57 58 label11 = new QLabel("", this); 59 label11->setGeometry(120,129,18,19); 60 label11->setPixmap(pixmap1); 61 label11->resize(pixmap1.size()); 62 63 label12 = new QLabel("", this); 64 label12->setGeometry(149,129,18,19); 65 label12->setPixmap(pixmap2); 66 label12->resize(pixmap2.size()); 67 68 label13 = new QLabel("", this); 69 label13->setGeometry(178,129,18,19); 70 label13->setPixmap(pixmap3); 71 label13->resize(pixmap3.size()); 72 73 label21 = new QLabel("", this); 74 label21->setGeometry(120,159,18,19); 75 label21->setPixmap(pixmap1); 76 label21->resize(pixmap1.size()); 77 78 label22 = new QLabel("", this); 79 label22->setGeometry(149,159,18,19); 80 label22->setPixmap(pixmap2); 81 label22->resize(pixmap2.size()); 82 83 label23 = new QLabel("", this); 84 label23->setGeometry(178,159,18,19); 85 label23->setPixmap(pixmap3); 86 label23->resize(pixmap3.size()); 87 } 88 89 void MyButtonWindow::radioChange() 90 { 91 QPixmap pixmap1(":/btn1_gray"); 92 QPixmap pixmap2(":/btn2_gray"); 93 QPixmap pixmap3(":/btn3_gray"); 94 label11->setPixmap(pixmap1); 95 label11->resize(pixmap1.size()); 96 label12->setPixmap(pixmap2); 97 label12->resize(pixmap2.size()); 98 label13->setPixmap(pixmap3); 99 label13->resize(pixmap3.size()); 100 101 QString url; 102 QLabel *label; 103 if (sender() == radio1) 104 { 105 url = ":/btn1_bright"; 106 label = label11; 107 } 108 else if (sender() == radio2) 109 { 110 url = ":/btn2_bright"; 111 label = label12; 112 } 113 else if (sender() == radio3) 114 { 115 url = ":/btn3_bright"; 116 label = label13; 117 } 118 119 QPixmap pixmap(url); 120 label->setPixmap(pixmap); 121 label->resize(pixmap.size()); 122 } 123 124 void MyButtonWindow::checkChange() 125 { 126 QString url; 127 QLabel *label; 128 if (sender() == check1) 129 { 130 url = check1->isChecked() ? ":/btn1_bright" : ":/btn1_gray"; 131 label = label21; 132 } 133 else if (sender() == check2) 134 { 135 url = check2->isChecked() ? ":/btn2_bright" : ":/btn2_gray"; 136 label = label22; 137 } 138 else if (sender() == check3) 139 { 140 url = check3->isChecked() ? ":/btn3_bright" : ":/btn3_gray"; 141 label = label23; 142 } 143 144 QPixmap pixmap(url); 145 label->setPixmap(pixmap); 146 label->resize(pixmap.size()); 147 }
main.cpp
1 #include <QApplication> 2 #include <QTextCodec> 3 #include "mybuttonwindow.h" 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 QTextCodec::setCodecForTr(QTextCodec::codecForName("System")); //消除乱码 9 10 MyButtonWindow buttonWindow; 11 buttonWindow.show(); 12 13 return a.exec(); 14 }
运行结果示例:
转自: