1. Basic usage of QT1

1. What is QT

Cross-platform C++ GUI application development framework

features

  • One framework, one code base, any platform deployment. (Strong cross-platform. Basically compatible with all devices: window, Linux, arm, iOS, Android, stm32, raspberry pie...)

2. Acquisition of QT software

1. Download through official channels

QT official download

2. Domestic Chinese community download

Domestic Chinese community download

  • Composition of QT: Composition of QT: QT SDK (a bunch of codes, C++ interface code encapsulated by QT)
  • QT Creator (integrated development tool officially provided by QT, which includes SDK and development tools with interface)

3. Use of QT Creator software

1. Create a new QT project

insert image description here
2. Create a QT form application

insert image description here
3. Set the project path name

insert image description here

4. Set the class name

insert image description here
5. Compiler selection

insert image description here

4. Engineering analysis of QT

insert image description here
1. xxxx.pro project configuration file

QT += core gui widgets   #添加QT的三大模块
core 核心模块:提供信号与槽、事件.......
gui 界面模块
widgets	窗体模块

CONFIG += c++11	#使用c++11编译器进行代码编译
SOURCES += \  #添加源文件
main.cpp \
mainwindow.cpp

HEADERS += \ #添加头文件
mainwindow.h

FORMS += \ #添加界面
mainwindow.ui

2. Analysis of xxxxx.h header file

//QMainWindow类的头文件
#include <QMainWindow>
//开始定义一个命名空间
QT_BEGIN_NAMESPACE
namespace Ui {
    
    class MainWindow; }
QT_END_NAMESPACE

//定义一个MainWindow 继承 QMainWindow
class MainWindow : public QMainWindow
{
    
    
	//QT中信号与槽的宏定义
	Q_OBJECT
public:
	//构造函数
	MainWindow(QWidget *parent = nullptr);
	//析构函数
	~MainWindow();
private:
	Ui::MainWindow *ui;//定义一个UI窗体
};

3. Main.cpp file analysis

//添加用户自定义窗体头文件
#include "mainwindow.h"
//添加QT应用类
#include <QApplication>
//主函数
int main()
{
    
    
	//创建QT应用对象
	QApplication a(argc, argv);
	//在应用中创建一个窗体
	MainWindow w;
	//显示窗体
	w.show();
	
	//执行应用
	return a.exec();//一直轮询这个应用
}

Notice:

  • There must be an application before the form, and all forms are on the application

4. Analysis of xxxxx.cpp file

#include "mainwindow.h"

//添加ui_mainwindow.h界面设计头文件,所有的界面设计都在该文件中进行
#include "ui_mainwindow.h"//不需要用户管,自动生成

//构造函数
MainWindow :: MainWindow(QWdiget *parent) : QMainWindow(parent), ui(new Ui :: MainWindow)
{
    
    
	//调用 设置UI的函数
	ui->setupUI(this);//void setupUi(QMainWindow *MainWindow)
}
//释放分配的空间 ui(new Ui::MainWindow)
MainWindow::~MainWindow()
{
    
    
	delete ui;
}

5. Create QT project by hand

1、

insert image description here
2. Add QT module

QT += core gui widgets  #添加QT三大模块
TEMPLATE = app
CONFIG += c++11
SOURCES += \
main.cpp

3. Use the official visual interface designer provided by QT to design the interface

#include <iostream>
//添加QT的应用
#include <QApplication>
//添加主界面的头文件
#include <QMainWindow>
//添加标签头文件
#include <QLabel>
//添加字体头文件
#include <QFont>

using namespace std;



int main(int argc, char **argv)
{
    
    
    cout << "Hello World!" << endl;
    //定义一个QT应用
    QApplication a(argc, argv);

    //添加一个主界面
    QMainWindow mywin;

    //设置窗体的大小
    mywin.setGeometry(200, 200, 800, 480);

    //定义一个标签,放在主窗体
    QLabel lb(&mywin);

    //设置标签的字体
    lb.setText("Hello World");

    //设置标签的位置
    lb.setGeometry(400, 200, 400, 100);

    //设置标签的大小
    QFont f("宋体", 20);
    lb.setFont(f);

    //显示主窗体
    mywin.show();

    //执行应用
    return a.exec();
}

6. UI design (controls) in QT

1. Open the UI control interface

insert image description here
2. Control interface

insert image description here

3. Selection of controls

insert image description here
4. Property setting

insert image description here

Exercise: Set the student's name and student number, and display it in the form
①, use a Label control

②, write code in mainwindow.cpp source code

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    ui->label->setText("你好");//设置字符串
    ui->label->setNum(123456);//设置数字
    ui->label->setPixmap(QPixmap("D:/picture/1.jpg"));//设置图片位置
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

③, Qlabel common interface

void clear() //清空qlabel
void setMovie(QMovie *movie) //设置一张动态图
void setNum(int num) //设置数字
void setNum(double num) //设置浮点型数据
void setPicture(const QPicture &picture) //设置图片
void setPixmap(const QPixmap &) //设置图片
void setText(const QString &) //设置文字

Seven, the use of help documents in QT

1. Search for the class you are looking for

insert image description here
2. Requirements for the use of display classes

insert image description here

Eight, the use of QString class

1. View some modules required by the class

The QString class provides a Unicode character string. More...
Header:
#include <QString> //头文件
qmake:
QT += core  //模块

//构造函数
QString::QString(const char *str)把一个字符串赋值到 QString 中
QString &append(const char *str) //追加字符串arg() //字符串拼接
例子:
QString msg1 = QString("%1 %2 %3").arg(123).arg("hello").arg(3.14);
void QString::chop(int n) //从右边开始删除字符
int indexOf(const QString &str, int from =0) const //从from的位置开始查找str
int lastIndexOf(const QString &str, intfrom = -1,) const //最后一次出现的位置
QString &insert(int position, const QString&str) //从position位置插入字符串str
QString QString::left(int n) const //取左边4 n个字符
int length() const //返回字符串的长度
QString QString::mid(int position, int n =-1) const //取中间部分字符串
QString &remove(const QString &str) //直接删除 str 字符串
QString &replace(const QString &before,const QString &after) //字符串替换

类型转换:
double toDouble(bool *ok = nullptr) const
float toFloat(bool *ok = nullptr) const
QByteArray toUtf8() const //转换为linux 的文本格式
QString number(long n, int base = 10)//把整形转换为 QSTRING 类型
例子:
//把整形转换为QString 字符串
QString t = QString::number(a);
qDebug() << t;
qDebug() << t.toInt() + 100;

Common functions of the QString class

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QString>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);

    QString msg = "hello";//定义一个字符类型数据
    qDebug() << msg;//  qDebug()打印信息

    msg.append(" world");//追加字符
    qDebug() << msg;

    QString msg1 = QString("%1 %2 %3").arg(123).arg("hello").arg(3.14);//拼接字符
    qDebug() << msg1;

    qDebug() << msg1.indexOf("hello");//返回字符最先出现的位置

    msg1.replace("hello", "你好");//替换字符
    qDebug() << msg1;

    int a = 10086;
    //把整形转换为字符串
    QString msg2 = QString :: number(a);
    qDebug() << msg2;

    qDebug() << msg2.toInt() + 100;
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

9. Switching of windows in QT

1. Add a new form file in QT

insert image description here

2. Select a new form

insert image description here

3. Modify the name of the new form

insert image description here
4. Realize the jump between two forms

1.新建一个窗体
w = new mywin(this); //记得传递this
,给子窗体,否则无法跳转回来
2.显示新窗体
w->show();
this->hide();
3.跳转回来
//在第二个窗体中,找到父亲窗体
this->parentWidget()->show();
//隐藏当前窗体
this->hide();

Guess you like

Origin blog.csdn.net/qq_53402930/article/details/132474986
Recommended