三个文件
自动生成函数的外形
在头文件中写好函数的声明之后,右键-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文件不做修改