QT (1) Installation & QT (2) GUI programming basis

Chapter 1: Qt Installation

  • download link
  • Install

image-20230622210955202

  • Open cmd to run the image:qt-unified-windows-x64-4.6.0-online.exe --mirror https://mirrors.aliyun.com/qt

Hello

image-20230622213939748

image-20230622222539982

image-20230622222917830

image-20230622223321256

  • Because it is qmake so it is.pro
  • Ctrl + Rrun directly

Chapter 2 Basics of GUI Programming

image-20230623113138523

image-20230623114818100

image-20230623115400866

main文件

image-20230623120632798

  • *.ui: Automatically generated by the UI designer, the name of the stored component and layout class is

  • ui_widget.h: Automatically generated according to component, signal ui widget.h: number and signal slot

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-9WEMX9Gy-1687505105908) (C:\Users\27285\AppData\Roaming\Typora\typora-user-images\ image-20230623121420295.png)]

image-20230623132814588

2.2 Visual UI design

insert image description here
insert image description here

signal slot

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>

class QCheckBox;
class QPushButton;
class QPlainTextEdit;
class QRadioButton;

QT_BEGIN_NAMESPACE
namespace Ui {
    
     class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    
    
    Q_OBJECT
public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();
private slots: // 槽 : 基本都是void 函数
    void do_chkBoxUnder(bool checked);
    void do_chkBoxItalic(bool checked);
    void do_chkBoxBold(bool checked);
    void do_setFontColor();
private:
    QCheckBox *chkBoxUnder;
    QCheckBox *chkBoxItalic;
    QCheckBox *chkBoxBold;

    QRadioButton *radioBlack;
    QRadioButton *radioRed;
    QRadioButton *radioBlue;

    QPlainTextEdit *txtEdit;

    QPushButton *btnOK;
    QPushButton *btnCancel;
    QPushButton *btnClose;
};
#endif // DIALOG_H

  • dialog.cpp

#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDialog>
#include <QCheckBox>
#include <QRadioButton>
#include <QPlainTextEdit>
#include <QPushButton>


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

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

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

void Dialog::do_setFontColor() {
    
    
    QPalette plet = txtEdit->palette();
    if(radioBlack->isChecked()) plet.setColor(QPalette::Text, Qt::black);
    if(radioRed->isChecked()) plet.setColor(QPalette::Text, Qt::red);
    if(radioBlue->isChecked()) plet.setColor(QPalette::Text, Qt::blue);
    txtEdit->setPalette(plet);
}

Dialog::Dialog(QWidget *parent ) : QDialog(parent) {
    
    
    chkBoxUnder = new QCheckBox("下划线");
    chkBoxItalic = new QCheckBox("斜体");
    chkBoxBold = new QCheckBox("加粗");
    QHBoxLayout *Hlayl = new QHBoxLayout();
    Hlayl->addWidget(chkBoxUnder);
    Hlayl->addWidget(chkBoxBold);
    Hlayl->addWidget(chkBoxItalic);

    radioBlack = new QRadioButton("黑色");
    radioRed= new QRadioButton("红色");
    radioBlue= new QRadioButton("蓝色");
    QHBoxLayout * Hlay2 = new QHBoxLayout();
    Hlay2->addWidget(radioBlack);
    Hlay2->addWidget(radioRed);
    Hlay2->addWidget(radioBlue);

    txtEdit = new QPlainTextEdit;
    txtEdit->setPlainText("HelloWorld\n 手工创建!");
    QFont font = txtEdit->font();
    font.setPointSize(20);
    txtEdit->setFont(font);

    btnOK = new QPushButton("确定");
    btnCancel = new QPushButton("取消");
    btnClose = new QPushButton("关闭");
    QHBoxLayout *Hlay3 = new QHBoxLayout();
    Hlay3->addWidget(btnOK);
    Hlay3->addStretch();
    Hlay3->addWidget(btnCancel);
    Hlay3->addStretch();
    Hlay3->addWidget(btnClose);

    QVBoxLayout *Vlay = new QVBoxLayout();
    // add 后会对内存进行回收
    Vlay->addLayout(Hlayl);
    Vlay->addLayout(Hlay2);
//    Vlay->addLayout(font);
    Vlay->addWidget(txtEdit);
    Vlay->addLayout(Hlay3);
    setLayout(Vlay);

    // 设置槽
    connect(chkBoxUnder,SIGNAL(clicked(bool)), this, SLOT(do_chkBoxUnder(bool)));
    connect(chkBoxItalic,SIGNAL(clicked(bool)), this, SLOT(do_chkBoxItalic(bool)));
    connect(chkBoxBold,SIGNAL(clicked(bool)), this, SLOT(do_chkBoxBold(bool)));

    connect(radioBlack, SIGNAL(clicked(bool)), this, SLOT(do_setFontColor()));
    connect(radioRed, SIGNAL(clicked(bool)), this, SLOT(do_setFontColor()));
    connect(radioBlue, SIGNAL(clicked(bool)), this, SLOT(do_setFontColor()));

    connect(btnOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
    connect(btnClose, SIGNAL(clicked()), this, SLOT(close()));

    setWindowTitle("people UI !");
}

Dialog::~Dialog()
{
    
    
    //    delete ui;
}

insert image description here
insert image description here

Use CMakeGUI to create a VS project

Tips for using Qt Creator

insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_49486457/article/details/131351112