天天看點

QT5全code程式設計

三個檔案

QT5全code程式設計

自動生成函數的外形

在頭檔案中寫好函數的聲明之後,右鍵-refactor-add definition, 将在指定檔案生成一個函數名一組花括号

代碼

1 //dialog頭檔案
 2 #ifndef DIALOG_H
 3 #define DIALOG_H
 4 
 5 //1 元件的頭檔案,與元件同名
 6 #include <QDialog>
 7 #include<QCheckBox>
 8 #include<QRadioButton>
 9 #include<QPushButton>
10 #include<QPlainTextEdit>
11 
12 class Dialog : public QDialog
13 {
14     Q_OBJECT
15 
16 public:
17     Dialog(QWidget *parent = 0);
18     ~Dialog();
19 
20  private:
21      //0 用類對象的方式,生成元件
22     QCheckBox *checkBox_Underline, *checkBox_Italic, *checkBox_Bold;
23     QRadioButton *rBtn_Black, *rBtn_Blue, *rBtn_Red;
24     QPlainTextEdit *plainTextEdit;
25     QPushButton *btn_OK, *btn_Cancel, *btn_Close;
26 
27     //3 自定義函數,右鍵-refactor-add definition
28     void iniUI();
29     void iniSignalSlots();
30 
31  //2 建立槽
32  private slots:
33     void on_checkUnderline(bool checked);
34     void on_checkItalic(bool checked);
35     void on_checkBold(bool checked);
36 
37     void setTextFntColor();
38 };
39 
40 #endif // DIALOG_H      

對應檔案

1 #include "dialog.h"
  2 #include<QHBoxLayout>
  3 #include<QVBoxLayout>
  4 
  5 
  6 //1 本應該在構造函數裡面初始化的内容
  7 void Dialog::iniUI()
  8 {
  9     //下劃線、斜體、加粗
 10     checkBox_Underline = new QCheckBox(tr("Underline"));
 11     checkBox_Italic = new QCheckBox(tr("Italic"));
 12     checkBox_Bold  = new QCheckBox(tr("Bold"));
 13     QHBoxLayout *HLayout1 = new QHBoxLayout;
 14     HLayout1->addWidget(checkBox_Underline);
 15     HLayout1->addWidget(checkBox_Italic);
 16     HLayout1->addWidget(checkBox_Bold);
 17 
 18     //字型顔色
 19     rBtn_Black = new QRadioButton("Black");
 20     rBtn_Blue = new QRadioButton("Blue");
 21     rBtn_Red = new QRadioButton("Red");
 22     QHBoxLayout *HLayout2 = new QHBoxLayout;
 23     HLayout2->addWidget(rBtn_Black);
 24     HLayout2->addWidget(rBtn_Blue);
 25     HLayout2->addWidget(rBtn_Red);
 26 
 27     //文本框
 28     plainTextEdit = new QPlainTextEdit();
 29     QFont font = plainTextEdit->font();
 30     font.setPointSize(20);
 31     plainTextEdit -> setFont(font);
 32     plainTextEdit -> setPlainText("Hello World!\n\nToday I am really sleepy...\n\nSorry.");
 33 
 34     //确認、取消、關閉
 35     btn_OK = new QPushButton("OK");
 36     btn_Cancel = new QPushButton("Cancel");
 37     btn_Close = new QPushButton("Close");
 38     QHBoxLayout *HLayout3 = new QHBoxLayout;
 39     HLayout3->addWidget(btn_OK);
 40     HLayout3->addWidget(btn_Cancel);
 41     HLayout3->addWidget(btn_Close);
 42 
 43     //排列組合
 44     QVBoxLayout *VLayout = new QVBoxLayout;
 45     VLayout->addLayout(HLayout1);
 46     VLayout->addLayout(HLayout2);
 47     VLayout->addWidget(plainTextEdit);
 48     VLayout->addLayout(HLayout3);
 49     setLayout(VLayout);
 50 }
 51 
 52 //4 關聯,最後一步
 53 void Dialog::iniSignalSlots()
 54 {
 55     connect(btn_OK, SIGNAL(clicked()), this , SLOT(accept()));
 56     connect(btn_Cancel, SIGNAL(clicked()), this , SLOT(reject()));
 57     connect(btn_Close, SIGNAL(clicked()), this , SLOT(close()));
 58 
 59     connect(checkBox_Underline, SIGNAL(clicked(bool)), this, SLOT(on_checkUnderline(bool)));
 60     connect(checkBox_Italic, SIGNAL(clicked(bool)), this, SLOT(on_checkItalic(bool)));
 61     connect(checkBox_Bold, SIGNAL(clicked(bool)), this, SLOT(on_checkBold(bool)));
 62 
 63     connect(rBtn_Red, SIGNAL(clicked(bool)), this, SLOT(setTextFntColor()));
 64     connect(rBtn_Blue, SIGNAL(clicked(bool)), this, SLOT(setTextFntColor()));
 65     connect(rBtn_Black, SIGNAL(clicked(bool)), this, SLOT(setTextFntColor()));
 66 }
 67 
 68 //2 槽-下劃線
 69 void Dialog::on_checkUnderline(bool checked)
 70 {
 71     QFont font = plainTextEdit->font();
 72     font.setUnderline(checked);
 73     plainTextEdit -> setFont(font);
 74 }
 75 
 76 void Dialog::on_checkItalic(bool checked)
 77 {
 78     QFont font = plainTextEdit->font();
 79     font.setItalic(checked);
 80     plainTextEdit -> setFont(font);
 81 }
 82 
 83 void Dialog::on_checkBold(bool checked)
 84 {
 85     QFont font = plainTextEdit->font();
 86     font.setBold(checked);
 87     plainTextEdit -> setFont(font);
 88 }
 89 
 90 //3 槽-字型顔色
 91 void Dialog::setTextFntColor()
 92 {
 93     QPalette plet = plainTextEdit->palette();
 94     if(rBtn_Black->isChecked())
 95         plet.setColor(QPalette::Text, Qt::black);
 96     else if(rBtn_Blue->isChecked())
 97         plet.setColor(QPalette::Text, Qt::blue);
 98     else if(rBtn_Red->isChecked())
 99          plet.setColor(QPalette::Text, Qt::red);
100     else
101         plet.setColor(QPalette::Text, Qt::black);
102 
103     plainTextEdit->setPalette(plet);
104 }
105 
106 //0 将初始化内容另外建立函數,構造函數隻留函數名
107 Dialog::Dialog(QWidget *parent)
108     : QDialog(parent)
109 {
110     iniUI();
111     iniSignalSlots();
112 }
113 
114 Dialog::~Dialog()
115 {
116 
117 }      

main檔案不做修改

參考

繼續閱讀