QT系列之 程式碼搭建UI介面
阿新 • • 發佈:2022-05-06
最終效果圖:【文章最後會放上程式碼】
一:不要勾選Generate Form
二:標頭檔案中宣告 控制元件 宣告初始化UI介面的函式
三:構建控制元件
四:宣告槽函式,定義槽函式
程式碼:
dialog.h檔案
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QCheckBox>
#include<QRadioButton>
#include<QPushButton>
#include<QPlainTextEdit>
class Dialog : public QDialog
{
Q_OBJECT
private:
QCheckBox *checkBoxUnderLine;
QCheckBox *checkBoxItalic;
QCheckBox *checkBoxBold;
QRadioButton *rBtnBlack;
QRadioButton *rBtnBlue;
QRadioButton *rBtnRed;
QPlainTextEdit *plainTxtEdit;
QPushButton *btnOK;
QPushButton *btnCancel;
QPushButton *btnClose;
void initUI();
void initSignalSlot();
private slots:
void onCheckUnderline(bool checked);
void onCheckItalic(bool checked);
void onCheckBold(bool checked);
void setTextFontColor();
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
};
#endif // DIALOG_H
dialog.cpp檔案
#include "dialog.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
void Dialog::initUI()
{
checkBoxUnderLine = new QCheckBox(tr("UnderLine"));
checkBoxItalic = new QCheckBox(tr("Italic"));
checkBoxBold= new QCheckBox(tr("Bold"));;
QHBoxLayout *HLayout1 = new QHBoxLayout;
HLayout1->addWidget(checkBoxUnderLine);
HLayout1->addWidget(checkBoxItalic);
HLayout1->addWidget(checkBoxBold);
rBtnBlack = new QRadioButton(tr("Black"));
rBtnBlue = new QRadioButton(tr("Blue"));
rBtnRed = new QRadioButton(tr("Red"));
QHBoxLayout *HLayout2 = new QHBoxLayout;
HLayout2->addWidget(rBtnBlack);
HLayout2->addWidget(rBtnBlue);
HLayout2->addWidget(rBtnRed);
plainTxtEdit = new QPlainTextEdit();
QFont font = plainTxtEdit->font();
font.setPointSize(20);
plainTxtEdit->setFont(font);
plainTxtEdit->setPlainText("Hello Allen\n This is a sample");
btnOK = new QPushButton(tr("ok"));
btnCancel = new QPushButton(tr("Cancel"));
btnClose = new QPushButton(tr("Close"));
QHBoxLayout *HLayout3 = new QHBoxLayout;
HLayout3->addStretch();
HLayout3->addWidget(btnOK);
HLayout3->addWidget(btnCancel);
HLayout3->addStretch();
HLayout3->addWidget(btnClose);
QVBoxLayout *VLayout = new QVBoxLayout;
VLayout->addLayout(HLayout1);
VLayout->addLayout(HLayout2);
VLayout->addWidget(plainTxtEdit);
VLayout->addLayout(HLayout3);
setLayout(VLayout);
}
void Dialog::initSignalSlot()
{
connect(btnOK,SIGNAL(clicked()),this,SLOT(accept()));
connect(btnCancel,SIGNAL(clicked()),this,SLOT(reject()));
connect(btnClose,SIGNAL(clicked()),this,SLOT(close()));
connect(checkBoxUnderLine,SIGNAL(clicked(bool)),this,SLOT(onCheckUnderline(bool)));
connect(checkBoxItalic,SIGNAL(clicked(bool)),this,SLOT(onCheckItalic(bool)));
connect(checkBoxBold,SIGNAL(clicked(bool)),this,SLOT(onCheckBold(bool)));
connect(rBtnBlack,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
connect(rBtnBlue,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
connect(rBtnRed,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
}
void Dialog::onCheckUnderline(bool checked)
{
QFont font = plainTxtEdit->font();
font.setUnderline(checked);
plainTxtEdit->setFont(font);
}
void Dialog::onCheckItalic(bool checked)
{
QFont font = plainTxtEdit->font();
font.setItalic(checked);
plainTxtEdit->setFont(font);
}
void Dialog::onCheckBold(bool checked)
{
QFont font = plainTxtEdit->font();
font.setBold(checked);
plainTxtEdit->setFont(font);
}
void Dialog::setTextFontColor()
{
QPalette plet = plainTxtEdit->palette();
if(rBtnBlack->isChecked())
plet.setColor(QPalette::Text,Qt::black);
else if(rBtnBlue->isChecked())
plet.setColor(QPalette::Text,Qt::blue);
else if(rBtnRed->isChecked())
plet.setColor(QPalette::Text,Qt::red);
else
plet.setColor(QPalette::Text,Qt::black);
plainTxtEdit->setPalette(plet);
}
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
initUI();
initSignalSlot();
}
Dialog::~Dialog()
{
}