Qt小程序(1)

Aki 发布于 2024-01-03 345 次阅读


#ifndef QWDIALOG_H
#define QWDIALOG_H
#include <QDialog>
#include <QCheckBox>
#include <QRadioButton>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

class qwdialog : public QDialog
{
    Q_OBJECT

public:
    qwdialog(QWidget *parent = nullptr);
    ~qwdialog();


    //声明槽函数
private slots:

    void  on_clicked_AckButton();

    void  on_clicked_CancleButton();

    void  on_clicked_ExitButton();

    void  setTextColor();

    void on_Underline_CheckBox();

    void on_Italic_CheckBox();

    void on_Bold_CheckBox();

private:

    //三个普通按钮
    QPushButton* AckButton;
    QPushButton* CancleButton;
    QPushButton* ExitButton;

    //三个颜色点击按钮
    QRadioButton* RedButton;
    QRadioButton* BlackButton;
    QRadioButton* BlueButton;

    //三个字体颜色按钮
    QCheckBox* UnderlineCheckBox;
    QCheckBox* ItalicCheckBox;
    QCheckBox* BoldCheckBox;

    //一个文本输入框
    QPlainTextEdit* txtEdit;

    //UI界面设计函数
    void setUI();

    //连接槽函数
    void setFunction();

};

#endif // QWDIALOG_H
#include "qwdialog.h"

qwdialog::qwdialog(QWidget *parent) : QDialog(parent)
{
    //调用
    setUI();
    setFunction();
}

qwdialog::~qwdialog()
{
    delete BoldCheckBox;
    delete ItalicCheckBox;
    delete UnderlineCheckBox;
    delete AckButton;
    delete CancleButton;
    delete ExitButton;
    delete RedButton;
    delete BlueButton;
    delete BlackButton;
    delete txtEdit;
}


//UI界面设计函数实现
void qwdialog::setUI()
{

    //三个字体格式
    UnderlineCheckBox = new QCheckBox(tr("Underline"));
    ItalicCheckBox = new QCheckBox(tr("Italic"));
    BoldCheckBox = new QCheckBox(tr("Bold"));
    //设置水平布局
    QHBoxLayout* Hlay1= new QHBoxLayout;
    Hlay1->addWidget(UnderlineCheckBox);
    Hlay1->addWidget(ItalicCheckBox);
    Hlay1->addWidget(BoldCheckBox);

    //三个颜色按钮
    RedButton = new QRadioButton(tr("Red"));
    BlueButton = new QRadioButton(tr("Blue"));
    BlackButton = new QRadioButton(tr("Black"));
    //设置水平布局
    QHBoxLayout* Hlay2 = new QHBoxLayout();
    Hlay2->addWidget(RedButton);
    Hlay2->addWidget(BlueButton);
    Hlay2->addWidget(BlackButton);

    //三个功能按钮
    AckButton = new QPushButton(tr("确认"));
    CancleButton = new QPushButton(tr("取消"));
    ExitButton = new QPushButton(tr("退出"));
    //设置水平布局
    QHBoxLayout* Hlay3 = new QHBoxLayout();
    Hlay3->addWidget(AckButton);
    Hlay3->addWidget(CancleButton);
    Hlay3->addWidget(ExitButton);

    //文本输入框
    txtEdit = new QPlainTextEdit();
    txtEdit->setPlainText("Hello,World!");
    QFont font = txtEdit->font();
    font.setPointSize(20);
    txtEdit->setFont(font);

    //创建垂直布局,设置为主布局
    QVBoxLayout* Vlay = new QVBoxLayout;
    Vlay->addLayout(Hlay1);
    Vlay->addLayout(Hlay2);
    Vlay->addWidget(txtEdit);
    Vlay->addLayout(Hlay3);

    //设置窗口的主要布局
    setLayout(Vlay);

    //设置窗口初始位置和大小,可以拖动改变大小
    setGeometry(1000,500,800,600);

    //设置窗口最小大小
    setMinimumSize(QSize(800,600));

    //设置窗口标题
    setWindowTitle("Test!!!");

}

//绑定信号和槽函数
void qwdialog::setFunction()
{
    connect(AckButton,SIGNAL(clicked()),this,SLOT(on_clicked_AckButton()));
    connect(CancleButton,SIGNAL(clicked()),this,SLOT(on_clicked_CancleButton()));
    connect(ExitButton,SIGNAL(clicked()),this,SLOT(on_clicked_ExitButton()));
    connect(RedButton,SIGNAL(clicked()),this,SLOT(setTextColor()));
    connect(BlackButton,SIGNAL(clicked()),this,SLOT(setTextColor()));
    connect(BlueButton,SIGNAL(clicked()),this,SLOT(setTextColor()));
    connect(UnderlineCheckBox,SIGNAL(clicked()),this,SLOT(on_Underline_CheckBox()));
    connect(ItalicCheckBox,SIGNAL(clicked()),this,SLOT(on_Italic_CheckBox()));
    connect(BoldCheckBox,SIGNAL(clicked()),this,SLOT(on_Bold_CheckBox()));
}

//Ack按钮槽函数
void qwdialog::on_clicked_AckButton()
{
    accept();
}

//Cancle按钮槽函数
void qwdialog::on_clicked_CancleButton()
{
    reject();
}

//Exit按钮槽函数
void qwdialog::on_clicked_ExitButton()
{
    close();
}

//字体颜色槽函数
void qwdialog::setTextColor()
{
    //QPalette是画板,用来改变外观
    QPalette plet = txtEdit->palette();
    if(RedButton->isChecked())
    {
        plet.setColor(QPalette::Text,Qt::red);
    }
    else if(BlueButton->isChecked())
    {
        plet.setColor(QPalette::Text,Qt::blue);
    }
    else
    {
        plet.setColor(QPalette::Text,Qt::black);
    }
    txtEdit->setPalette(plet);
}

//字体格式槽函数
void qwdialog::on_Bold_CheckBox()
{
    QFont font = txtEdit->font();
    font.setBold(BoldCheckBox->isChecked());
    txtEdit->setFont(font);
}

//字体格式槽函数
void qwdialog::on_Italic_CheckBox()
{
    QFont font = txtEdit->font();
    font.setItalic(ItalicCheckBox->isChecked());
    txtEdit->setFont(font);
}

//字体格式槽函数
void qwdialog::on_Underline_CheckBox()
{
    QFont font = txtEdit->font();
    font.setUnderline(UnderlineCheckBox->isChecked());
    txtEdit->setFont(font);
}



#include "qwdialog.h"

#include <QApplication>

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