QT(1)

1. QT Introduction

Qt is a cross-platform application development framework for desktop, embedded, and mobile devices. The supported platforms include Linux, OS X, Windows, VxWorks, QNX, Android, iOS, BlackBerry (BlackBerry), Sailfish OS (Sailfish) Operating system) and so on.

Qt is a framework written based on C++. It can be used to develop GUI programs as well as non-GUI programs, such as console tools and servers. The preprocessor, MOC (full name: Meta-Object Compiler) is used to extend C++, such as the characteristics of signals and slots. Before compiling, use MOC to analyze the C++ source file of Qt extension (check whether it contains the Q_OBJECT macro), and then generate a source file that conforms to the C++ standard (the new file name will be composed of moc_ plus the original file name) . Therefore, the framework itself and the application/library can be compiled by any standard C++-compliant compiler (like Clang, GCC, ICC, MinGW and MSVC).

1.1 Download and install

QT download official website

2. The first QT program in life

2.1 Project directory

Insert picture description here

2.2 Test, run empty project

Insert picture description here

2.3 Add code

myfirstqt.cpp

#include "myfirstqt.h"
#include "QPushButton"


MyFirstQt::MyFirstQt(QWidget *parent)
    : QMainWindow(parent)
{
    
    
    QPushButton * btn = new QPushButton("按钮1",this);
    // 窗口大小
    this->resize(600,400);
    // 固定窗口大小
    // this->setFixedSize(600,400);
    // 窗口标题
    this->setWindowTitle("窗口标题");
    // 按钮大小
    btn->resize(200,100);
    // 按钮位置
    btn->move(200,200);

}

MyFirstQt::~MyFirstQt()
{
    
    
    QPushButton p;
}

2.4 Operation results:

Insert picture description here

3. Signals and slots

In addition to drawing controls, GUI programs also respond to system and user events, such as redrawing, drawing completion, clicking the mouse, and typing the keyboard. When an event occurs, the UI will change accordingly, allowing users to intuitively see

Most programming (such as Win SDK, Web front-end) use callback functions to respond to events, but Qt has created a signal and slot mechanism. The
so-called callback function is a function defined by the programmer in advance, and the function is called when an event occurs.

3.1 Basic case

Based on the above code, click the button to close the window

    // 连接信号 和 槽
    // 所谓的连接,就是当信号执行时,触发槽函数
    // 下面这个代码就是连接 按钮的点击事件,然后就是触发窗口的关闭
    connect(btn,&QPushButton::clicked,this,&MyFirstQt::close);

Insert picture description here

3.2 Custom signals and slots

The above case is the slot function provided to us by qt. Let's
create a custom slot function.
Requirements: Every time a button is clicked, the console outputs the number of clicks.

3.2.1 Create a custom slot class in the project

Insert picture description here

3.2.2 Define the slot function in the custom slot class
#ifndef CUSTOMSLOT_H
#define CUSTOMSLOT_H

#include <QObject>

class Customslot : public QObject
{
    
    
    Q_OBJECT
public:
    explicit Customslot(QObject *parent = 0);
    int cout; // 定义一个变量记录次数

signals:
    // 信号
    // 信号只需要声明,不需要实现
    // 信号可以有参数,可以发生重载
    // 信号函数 返回值 void
public slots:
    // 槽
    // 槽需要实现
    // 槽可以有参数,可以发生重载
    // 槽函数 返回值 void
    void addClickCount();
};

#endif // CUSTOMSLOT_H

Implement the slot function:
customslot.cpp

#include "customslot.h"
#include <QDebug> // 控制台输出需要包含这个头文件

Customslot::Customslot(QObject *parent) : QObject(parent)
{
    
    
}
void Customslot::addClickCount()
{
    
    
    qDebug() << "按钮被点击" << ++this->cout << "次";
}
3.3.3 Connecting signals and slots
    // 连接信号 和 槽
    // 所谓的连接,就是当信号执行时,触发槽函数
    // 下面这个代码就是连接 按钮的点击事件,然后就是触发窗口的关闭
    Customslot * slot = new Customslot(this); // this指的是父类,这样会由qt的对象树管理该对象,自动回收
    connect(btn,&QPushButton::clicked,slot,&Customslot::addClickCount);
3.3.4 Operation results

**Bold style**

3.3 Parameterized signals and slots

In the development process of Qt, the signal with parameters is very common. When using the signal slot with parameters, the following points need to be paid attention to

  • When the number of parameters of the signal and slot functions is the same, their parameter types must be exactly the same
  • When the parameters of the signal and the number of parameters of the slot function are different, the number of parameters of the signal can only be more than the number of parameters of the slot function , and the type of the same number of parameters should be the same, and the extra parameters in the signal will be ignored
  • When the parameter is not passed, the signal slot binding also requires that the number of parameters of the signal is greater than or equal to the number of parameters of the slot function . In this case, a signal with parameters is generally bound to a slot function without parameters.
3.3.1 First define a member parameter in MyFirstQt

Insert picture description here

3.3.2 Initialize member parameters

Insert picture description here

3.3.3 Building a parameterized signal function

Insert picture description here

3.3.4 Functions with parameter slots

Insert picture description here

3.3.5 Implement slot function

Insert picture description here

3.3.6 Declare a function to send a signal

Insert picture description here

3.3.7 Realize the function of sending signal

Insert picture description here

3.3.7 Connecting signals and slots

Insert picture description here

3.3.8 Running results

Insert picture description here

3.3.10 QString output is incorrect

Insert picture description here
It should QString first pass toUtf8()through the turn into QByteArray data()converted to char *

3.3.11 pit record

说一下这里我踩的坑,跟着视频中的老师敲得,但是我敲得跟老师敲得不大一样,所以出现了问题

In the first three positions:
Insert picture description here
I used an anonymous object:
Insert picture description here
the result is that the program running, but does not perform the function of the slot, where delayed for a long time, and finally chose the kind of writing teacher, found the problem here
last test several times:
first One time: The signal object and the signal sending object use the same object.
Insert picture description here
No problem. The
second time: The signal object and the slot object use the same object.
Insert picture description here
No, in
order to verify: The
third test:
Insert picture description here

come to conclusion:
绑定信号的对象 必须和发送 信号的对象是同一个

Guess you like

Origin blog.csdn.net/haiyanghan/article/details/113280119