[Five days] Qt from entry to actual combat: the first day

first day

1.1 Introduction, advantages and success stories of Qt

What is Qt?

Qt is aCross-platformC++ GUI application framework. She provides application developers with all the functions needed to build state-of-the-art graphical interfaces. It is fully object-oriented, easily extensible, and allows true component programming. Download link: https://download.qt.io/new_archive/qt/

Advantages of Qt

  • Cross-platform, supports almost all platforms
  • Simple interface, easy to use
  • To a certain extent, the memory recovery mechanism is simplified
  • High development efficiency, able to quickly build applications
  • Embedded development possible

Qt Success Stories

  • Linux desktop environment
  • WPS office office software
  • Skype VoIP
  • Google Map
  • VLC multimedia player
  • VirtualBox virtual machine software

1.2 Create the first Qt program

  • After clicking Create Project, select the project path and project name.
  • Name: no Chinese, no spaces
  • Path: no Chinese path
  • The window class is created by default: myWidget, and there are three options for the base class: QWidget, QMainWindow, QDialog
  • main function
    • QApplication a application object, there is only one
    • myWidget w instantiates the window object
    • w.show() calls the show function to display the window
    • return a.exec(); Let the application object enter the message loop mechanism, and the code blocks to the current line

1.3 Familiar with common APIs of button controls

#include "mywidget.h"
#include<QPushButton>

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)
{
    
    
    //创建一个按钮
    QPushButton *btn=new QPushButton;
    btn->show();//show以顶层方式弹出窗口控件
    //让btn对象依赖在myWidget窗口中
    btn->setParent(this);
    //显示文本
    btn->setText("第一个按钮");

    //创建第二个按钮 按照控件的大小创建窗口
    QPushButton * btn2=new QPushButton("第二个按钮",this);

    //移动btn2按钮
    btn2->move(100,100);

    //重置窗口大小
    resize(600,400);

    //固定窗口大小
    setFixedSize(600,400);

    //设置窗口标题
    setWindowTitle("第一个窗口");
}

myWidget::~myWidget()
{
    
    

}

1.4 Basic concept of object tree

  • When the created object is in the heap area, if the specified parent is a class derived from QObject or a class derived from a QObject subclass, it can be automatically put into the object tree without managing the release operation.
  • To a certain extent, the memory recovery mechanism is simplified.

1.5 Coordinate system in Qt

  • The upper left corner is 0, 0 points
  • The positive direction is to the right of x, and the positive direction is below y

1.6 Basic use and extension of signals and slots

  • connection function connect
    • Parameter 1: The sender of the signal
    • Parameter 2: The signal sent (function address)
    • Parameter 3: Receiver of the signal
    • Parameter 4: slot function to be processed (function address)
//需求 点击我的按钮 关闭窗口
    //参数1 信号的发送者 参数2 发送的信号(函数的地址)参数3 信号的接收者 参数4 处理的槽函数
    connect(myBtn,&QPushButton::clicked,this,&QWidget::close);
  • Custom Signals and Slots
    • Custom signal: write to Signals; return void; need to declare, not need to implement; can have parameters, but cannot be overloaded.
    • Custom slot function: returns void; requires declaration and implementation; can have parameters and can be overloaded; write to public slot or public or global function
//创建一个老师对象
    this->zt=new Teacher(this);

    //创建一个学生对象
    this->st=new Student(this);

	//老师饿了,学生请客的连接
	connect(zt,&Teacher::hungry,st,&Student::treat);

    //调用下课函数
    classIsOver();
  • When custom signals and slots are overloaded, function pointers need to be used to clearly point to the address of the function
	//连接带参数的信号和槽函数
    //指针->地址
    //函数指针->函数地址
    void(Teacher::*teacherSignal)(QString)=&Teacher::hungry;
    void(Student::*studentSlot)(QString)=&Student::treat;

    //老师饿了,学生请客的连接
    connect(zt,teacherSignal,st,studentSlot);

    //调用下课函数
    classIsOver();
  • QString converted to char*:
    • QString to QByteArray: .toUtf8()
    • QByteArray converted to char*: .data()
      QString: with brackets
      insert image description here
      char*: without brackets
      insert image description here
  • Signal can be connected to signal
  • A signal can connect to multiple slot functions
  • Multiple signals can be connected to the same slot function
  • The parameters of the signal and slot functions must have a one-to-one correspondence
  • Should the number of parameters of the signal and slot functions be the same? The number of parameters of the signal can be more than the number of parameters of the slot function
	//无参信号和槽连接
    void(Teacher::*teacherSignal2)(void)=&Teacher::hungry;
    void(Student::*studentSlot2)(void)=&Student::treat;
    connect(zt,teacherSignal2,st,studentSlot2);

    //信号连接信号
    connect(btn,&QPushButton::clicked,zt,teacherSignal2);
  • disconnect signal
	//断开信号
    disconnect(zt,teacherSignal2,st,studentSlot2);
  • Signal and slot connections before Qt4
//Qt4版本以前的信号和槽连接方式
    //利用Qt4信号和槽连接无参版本
    connect(zt,SIGNAL(hungry()),st,SLOT(treat()));

insert image description here

Guess you like

Origin blog.csdn.net/qq_40507857/article/details/125730739