Qt练习.代码化UI设计

//QWDlgManual.h
#pragma once
#include 
#include 
#include 
#include 
#include 

#include 
#include "ui_QWDlgManual.h"

class QWDlgManual : public QDialog
{
     
    Q_OBJECT

public:
    QWDlgManual(QWidget *parent = Q_NULLPTR);
    ~QWDlgManual();

private slots:
    void on_chkBoxUnderline(bool checked);
    void on_chkBoxItalic(bool checked);
    void on_chkBoxBold(bool checked);

    void setTextFontColor();

private:
    Ui::QWDlgManualClass ui;

    QCheckBox* chkBoxUnder;
    QCheckBox* chkBoxItalic;
    QCheckBox* chkBoxBold;

    QRadioButton* rBtnBlack;
    QRadioButton* rBtnRed;
    QRadioButton* rBtnBlue;

    QPlainTextEdit* txtEdit;

    QPushButton* btnOK;
    QPushButton* btnCancel;
    QPushButton* btnClose;

    //用于创建界面组件,完成布局和属性设置
    void iniUI();
    //用来完成信号与槽的关联
    void iniSignalSlots();
};
//QWDlgManual.cpp
#include 
#include 
#include 

#include "QWDlgManual.h"


QWDlgManual::QWDlgManual(QWidget *parent)
    : QDialog(parent)
{
     
    iniUI();
    iniSignalSlots();
    setWindowTitle("Keep trying");
}

QWDlgManual::~QWDlgManual()
{
     
}

void QWDlgManual::on_chkBoxBold(bool checked)
{
     
    QFont font = txtEdit->font();
    font.setBold(checked);
    txtEdit->setFont(font);
}

void QWDlgManual::on_chkBoxItalic(bool checked)
{
     
    QFont font = txtEdit->font();
    font.setItalic(checked);
    txtEdit->setFont(font);
}

void QWDlgManual::on_chkBoxUnderline(bool checked)
{
     
    QFont font = txtEdit->font();
    font.setUnderline(checked);
    txtEdit->setFont(font);
}

void QWDlgManual::setTextFontColor()
{
     
    QPalette plet = txtEdit->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);
    txtEdit->setPalette(plet);
}

void QWDlgManual::iniUI()
{
     
    chkBoxBold = new QCheckBox(tr("Bold"));
    chkBoxItalic = new QCheckBox(tr("Italic"));
    chkBoxUnder = new QCheckBox(tr("Underline"));
    QHBoxLayout* HLay1 = new QHBoxLayout;
    HLay1->addWidget(chkBoxBold);
    HLay1->addWidget(chkBoxItalic);
    HLay1->addWidget(chkBoxUnder);

    rBtnBlack = new QRadioButton(tr("Black"));
    rBtnBlack->setChecked(true);
    rBtnBlue = new QRadioButton(tr("Blue"));
    rBtnRed = new QRadioButton(tr("Red"));
    QHBoxLayout* HLay2 = new QHBoxLayout;
    HLay2->addWidget(rBtnBlack);
    HLay2->addWidget(rBtnBlue);
    HLay2->addWidget(rBtnRed);

    btnOK = new QPushButton(tr("OK"));
    btnCancel = new QPushButton(tr("Cancel"));
    btnClose = new QPushButton(tr("Close"));
    QHBoxLayout* HLay3 = new QHBoxLayout;
    HLay3->addWidget(btnOK);
    HLay3->addWidget(btnClose);
    HLay3->addWidget(btnCancel);

    txtEdit = new QPlainTextEdit;
    txtEdit->setPlainText("Just hold on.\nAnd  never give up!");
    QFont font = txtEdit->font();
    font.setPixelSize(15);
    txtEdit->setFont(font);

    QVBoxLayout* VLay = new QVBoxLayout;
    VLay->addLayout(HLay1);
    VLay->addLayout(HLay2);
    VLay->addWidget(txtEdit);
    VLay->addLayout(HLay3);
    setLayout(VLay);
}

void QWDlgManual::iniSignalSlots()
{
     
    connect(rBtnBlue, SIGNAL(clicked()), this, SLOT(setTextFontColor()));
    connect(rBtnBlack, SIGNAL(clicked()), this, SLOT(setTextFontColor()));
    connect(rBtnRed, SIGNAL(clicked()), this, SLOT(setTextFontColor()));

    connect(chkBoxBold, SIGNAL(clicked(bool)), this, SLOT(on_chkBoxBold(bool)));
    connect(chkBoxUnder, SIGNAL(clicked(bool)), this, SLOT(on_chkBoxUnderline(bool)));
    connect(chkBoxItalic, SIGNAL(clicked(bool)), this, SLOT(on_chkBoxItalic(bool)));

    connect(btnOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
    connect(btnClose, SIGNAL(clicked()), this, SLOT(close()));
}
//main.cpp
#include "QWDlgManual.h"
#include 

int main(int argc, char *argv[])
{
     
    QApplication a(argc, argv);
    QWDlgManual w;
    w.show();
    return a.exec();
}

你可能感兴趣的:(qt学习,qt)