1. Preface
In my last blog, I briefly introduced the object-oriented programming of C + +. This blog is mainly used to get started with Qt. Don't talk too much nonsense. Let's do it!
C + + foundation of Qt learning
2. General
In the learning process of Qt, I watch the video first, brush the general knowledge twice as fast, then put forward a demand for myself, and then use Qt to complete the project
Learning video: The latest QT complete version from introduction to actual combat | intelligence education
This video is very good. There are many common methods. It's suitable to get started quickly.
Then the early small project is to write a Qt based serial port debugging assistant
The following content is only for learning, and most of the content comes from videos!
The content of this blog is original and difficult to edit. Please indicate it!!!
3. Preparation
3.1. Installation of QT
You can see this blog about Qt installation. It is more detailed and suitable for Xiaobai. If you are a big man, you can download it directly from the official website, and then install it next.
QT5.11 download and installation tutorial
There is nothing to pay attention to in windows. Pay attention to adding libraries in the middle of installation. Pay attention to adding only required libraries.
3.2. Advantages of QT
Firstly, Qt is a cross platform graphical interface engine, which is applicable to Windows and Linux. My project is a small tool under the two systems. At that time, I also considered using Java. However, due to the low configuration of the second device, the software performance is insufficient and the delay is unacceptable. Finally, I chose to develop with Qt.
At the same time, compared with C + +, Qt has a good mechanism. It has its own set of memory recycling mechanism, which simplifies memory recycling to a certain extent. There is no need to always think about delete after new application. At the same time, Qt also has many successful development cases. For example, the Linux desktop environment KDE is developed with Qt.
3.3. Basic knowledge
3.3.1. Coordinate system of QT
The upper left corner is 0, 0 point
x is positive to the right
Positive direction below y
It's different from our usual mathematical rectangular coordinate system
3.3.2. Object tree
When the created object is in the heap, if the specified parent is a class derived from QObject or a class derived from a subclass of QObject, you can not manage the release operation. The object will be put into the object tree and recycled at the time of recycling, which simplifies the internal recycling mechanism to a certain extent
4. Create project
4.1. Create a new project
In the upper left corner, click new, and the interface will pop up
On the left side of this interface, select whether to create a new project or a single file. A single file is generally used to create a new class or a new ui interface. We create a new project, select Application, and then select the style in the middle. The first is a window program, the second is a console program, and we create a window program.
After selection, enter the project name. Note that the name cannot have Chinese or spaces, and then determine the path. Note that the path cannot have Chinese path.
Then go to the next step to this interface
The window class created by default is QMainWindow. The base classes you can select include Q Widget, QMainWindow and QDialog. One is a single window, the other is the main interface and the other is the dialog box. MainWindow has more common controls than widgets, such as menu bar.
4.2. Document directory
After a project is created, these files will be generated by default, with brief descriptions
4.2.1. Pro file
The pro file is equivalent to a configuration manifest
QT += core gui Qt Included modules greaterThan(QT_MAJOR_VERSION, 4): QT += widgets //Versions greater than 4 contain widget modules TARGET = MyTest //The name of the. exe program generated by the target TEMPLATE = app //Template Application template Application SOURCES += \ //source file main.cpp \ mainwindow.cpp HEADERS += \ //Header file mainwindow.h FORMS += \ //ui file interface mainwindow.ui
4.2.2. main.cpp
QApplication a(argc, argv); //Application object, with and only one MainWindow w; //Instantiate window object w.show(); //Call 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
4.2.3. mainwindow.cpp
This is the main place for writing logic code, together with mainwindow.h
4.2.4. mainwindow.ui
ui file, visual interface drag, interface layout can be done, which is very convenient
5. API
To learn a language, you should often check its API documents. Qt's API documents are written in great detail. There are two methods:
5.1. Embedded help
The first is to click the help button on the left, and then select find on the top to query the usage of the class you want to know.
5.2. Qt assistant
The second is in..... \ Qt5.9.5.9.5\mingw53_32\bin directory
The advantage of this method is that it is separated from the development tool, and there is no need to switch the interface in the development tool all the time for query.
6. Signal and slot
Signals and slots are a feature of Qt. When a class runs, calling signals will trigger the corresponding slot function. At the same time, the signal function can also pass parameters to the slot function.
6.1. Usage
Connection function: connect (parameter 1, parameter 2, parameter 3, parameter 4)
Valuepoint 1 sender of the signal
Parameter 2 signal sent (function address)
Parameter 3 receiver of signal
Parameter 4 slot function processed (address of function)
//Case of clicking the button to close the window connect(btn , &QPushButton::click,this,&QWidget::close );
6.2. Custom signal and slot function
Sometimes, only the signals and slots provided by the class are limited. We can also use our own defined signals and slot functions
Custom signal
Custom signals should be written to the signals under the header file. Sometimes the project is not automatically generated, and you write them yourself. The return value is void. You only need to declare, not implement.
It can have parameters and can also be overloaded
Custom slot function
The return value of the custom slot function is void. It needs to be declared and implemented. It can have parameters and can be overloaded. It should be written in the public slot or public or global function under the header file
6.2.1. Code
Look directly at the code implementation
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; signals: //signal void print(QString str); public slots: //Slot function void onClicked(); void printString(QString str); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::onClicked); connect(this,&MainWindow::print,this,&MainWindow::printString); } void MainWindow::onClicked() { if(ui->pushButton->text() == "Hello,World") { ui->pushButton->setText("An ordinary button"); } else { ui->pushButton->setText("Hello,World"); emit this->print("Hello,World"); } } void MainWindow::printString(QString str) { qDebug()<<"The string to print is:"<<str; } MainWindow::~MainWindow() { delete ui; }
mainwindow.ui
In the ui interface, directly drag a pushButton to the main interface.
The function is to click the button, and the button name will be converted before an ordinary button and Hello,World. At the same time, when the name is Hello,World, the console will print Hello,World once
At the same time, briefly explain the following:
This code binds the button click event to the onClicked function
connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::onClicked);
This code binds the print signal in the class to the slot function printString in the class
connect(this,&MainWindow::print,this,&MainWindow::printString);
When calling, use emit this - > Print ("Hello, world"); You can trigger the void printString(QString str) function
It's still very convenient to use.
6.3. Extension
6.3.1. Heavy load
Both signals and slots can be overloaded, and the overload usage is cumbersome and complex.
When the custom signal and slot are overloaded, you need to use the function pointer to point to the address of the function to tell the compiler which one is connected.
For example:
void( Teacher:: * tSignal )( QString ) = &Teacher::hungry;
Small tips
When QString needs to be converted to char *,
First convert it into QByteArray with ToUtf8() method, and then convert it into Char with Data()*
For example:
String str("0123456789"); char* p = str.toUtf8().data(); qDebug()<<p[1]; //Output 1
6.3.2. Others
If you need to disconnect, you need to use the disconnect function
At the same time, one signal can also be connected to another signal. One signal can be connected to multiple slot functions, and multiple signals can be connected to the same slot function
The parameter types of signal and slot function must correspond one to one, but the number of parameters of signal can be more than that of slot function
6.3.3. Qt4 version writing
connect( Sender of signal,Transmitted signal SIGNAL(signal),Signal receiver,Slot function SLOT(Slot function))
The advantage is that the parameters are more intuitive
The disadvantage is that the compiler does not detect parameter types
6.4. Lambda expression
Lambda expression is a simple way to write anonymous functions. If you want to know more about it, you can query it yourself.
Here is only the usage
connect(ui->pushButton,&QPushButton::clicked,[=](){ ui->pushButton->setText("Hello,World"); });
[] is an anonymous function
[] is value passing
[&] is passed by reference
() is a parameter
{} is an implementation
6.5. Summary of overview drawings
7. Summary
According to the video, this is the first day's learning task, mainly to be familiar with Qt grammar and the basic knowledge of Qt.
Unfinished to be continued = w=