Qt练习.代码化UI设计

Posted qq_51102350

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt练习.代码化UI设计相关的知识,希望对你有一定的参考价值。

//QWDlgManual.h
#pragma once
#include <qdialog.h>
#include <qcheckbox.h>
#include <qradiobutton.h>
#include <qplaintextedit.h>
#include <qpushbutton.h>

#include <QtWidgets/QDialog>
#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 <qfont.h>
#include <qpalette.h>
#include <qboxlayout.h>

#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 <QtWidgets/QApplication>

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

以上是关于Qt练习.代码化UI设计的主要内容,如果未能解决你的问题,请参考以下文章

pyqt5通过qt designer 设计方式连接多个UI图形界面

UI 设计代码化:低代码式设计语言 —— Unflow

UI 设计代码化:低代码式设计语言 —— Unflow

Qt编程中,怎么对用ui生成的图形控件操作

python_UI设计--------基本框架

无法运行Qt设计器编译的python代码[重复]