Qt实践-基于C++的UI设计基础
学习网址:http://c.biancheng.net/view/1824.html
我的代码:
头文件qwdlgmanual.h:
1 #ifndef QWDLGMANUAL_H 2 #define QWDLGMANUAL_H 3 4 #include <QDialog> 5 #include <QPushButton> 6 #include <QRadioButton> 7 #include <QCheckBox> 8 #include <QPlainTextEdit> 9 #include <QBoxLayout> 10 11 class QWDlgManual : public QDialog 12 { 13 Q_OBJECT 14 15 public: 16 QWDlgManual(QWidget *parent = nullptr); 17 ~QWDlgManual(); 18 19 private slots: 20 void on_setUnderline(bool checked); 21 void on_setBold(bool checked); 22 void on_setItalic(bool checked); 23 void ColorSet(); 24 25 private: 26 QRadioButton* rbtnred; 27 QRadioButton* rbtnblue; 28 QRadioButton* rbtnblack; 29 QCheckBox* ckboxBold; 30 QCheckBox* ckboxItalic; 31 QCheckBox* ckboxUnder; 32 QPushButton* btnClose; 33 QPushButton* btnOK; 34 QPlainTextEdit* textEdit; 35 void initialSignalSlots(); 36 void initialUI(); 37 }; 38 #endif // QWDLGMANUAL_H
qwdlgmanual.cpp:
1 #include "qwdlgmanual.h" 2 3 QWDlgManual::QWDlgManual(QWidget *parent) 4 : QDialog(parent) 5 { 6 initialUI(); 7 initialSignalSlots(); 8 setWindowTitle("Form"); 9 } 10 11 QWDlgManual::~QWDlgManual() 12 { 13 } 14 15 void QWDlgManual::on_setUnderline(bool checked) 16 { 17 QFont font=textEdit->font(); 18 font.setUnderline(checked); 19 textEdit->setFont(font); 20 } 21 22 void QWDlgManual::on_setBold(bool checked) 23 { 24 QFont font=textEdit->font(); 25 font.setBold(checked); 26 textEdit->setFont(font); 27 } 28 29 void QWDlgManual::on_setItalic(bool checked) 30 { 31 QFont font=textEdit->font(); 32 font.setItalic(checked); 33 textEdit->setFont(font); 34 } 35 36 void QWDlgManual::ColorSet() 37 { 38 QPalette palte=textEdit->palette(); 39 if(rbtnred->isChecked()) 40 palte.setColor(QPalette::Text,Qt::red); 41 else if(rbtnblue->isChecked()) 42 palte.setColor(QPalette::Text,Qt::blue); 43 else if(rbtnblack->isChecked()) 44 palte.setColor(QPalette::Text,Qt::black); 45 textEdit->setPalette(palte); 46 } 47 48 void QWDlgManual::initialSignalSlots() 49 { 50 connect(rbtnred,SIGNAL(clicked()),this,SLOT(ColorSet())); 51 connect(rbtnblue,SIGNAL(clicked()),this,SLOT(ColorSet())); 52 connect(rbtnblack,SIGNAL(clicked()),this,SLOT(ColorSet())); 53 //radiobutton与槽函数的链接 54 connect(ckboxBold,SIGNAL(clicked(bool)),this,SLOT(on_setBold(bool))); 55 connect(ckboxUnder,SIGNAL(clicked(bool)),this,SLOT(on_setUnderline(bool))); 56 connect(ckboxItalic,SIGNAL(clicked(bool)),this,SLOT(on_setItalic(bool))); 57 //checkbutton与槽函数的链接 58 connect(btnOK,SIGNAL(clicked()),this,SLOT(accept())); 59 connect(btnClose,SIGNAL(clicked()),this,SLOT(close())); 60 //pushbutton与槽函数的链接 61 } 62 63 void QWDlgManual::initialUI() 64 { 65 ckboxBold = new QCheckBox(tr("Bold")); 66 ckboxItalic = new QCheckBox(tr("Itlic")); 67 ckboxUnder = new QCheckBox(tr("Underline")); 68 QHBoxLayout* Hey=new QHBoxLayout; 69 Hey->addWidget(ckboxBold); 70 Hey->addWidget(ckboxItalic); 71 Hey->addWidget(ckboxUnder); 72 //增加更改字体的CheckBox 73 rbtnred = new QRadioButton(tr("Red")); 74 rbtnblue = new QRadioButton(tr("Blue")); 75 rbtnblack = new QRadioButton(tr("Black")); 76 QHBoxLayout* Hsy=new QHBoxLayout; 77 Hsy->addWidget(rbtnred); 78 Hsy->addWidget(rbtnblue); 79 Hsy->addWidget(rbtnblack); 80 //增加更改颜色的RadioButton 81 btnOK = new QPushButton(tr("确定")); 82 btnClose = new QPushButton(tr("关闭")); 83 QHBoxLayout* Hty=new QHBoxLayout; 84 Hty->addStretch(); 85 Hty->addWidget(btnOK); 86 Hty->addWidget(btnClose); 87 //添加确认与退出按键 88 textEdit = new QPlainTextEdit; 89 textEdit->setPlainText("Hello World!\nThis is my demo."); 90 QFont font = textEdit->font(); 91 font.setPointSize(20); 92 textEdit->setFont(font); 93 //设置文本格式 94 QVBoxLayout* Vey=new QVBoxLayout; 95 Vey->addLayout(Hey); 96 Vey->addLayout(Hsy); 97 Vey->addWidget(textEdit); 98 Vey->addLayout(Hty); 99 //垂直风格的页面总布局 100 setLayout(Vey);//设置为窗体的主布局 101 }
主文件main:
1 #include "qwdlgmanual.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 QWDlgManual w; 9 w.show(); 10 return a.exec(); 11 }