QT GUI programming!

1, QT basis

1.1 introduction to QT

1.1.1. What is QT

  • A bunch of C++/python class libraries (GUI classes, network,...)
  • Free open source

1.1.2 QT characteristics:

  • Excellent cross platform characteristics:
    Qt supports the following operating systems: Windows, Linux, Solaris, sun0s, FreebSD, BSD/S, SCO, AIX, 0S390, QNX, android, etc
  • object-oriented
    -Qt's good encapsulation mechanism makes Qt very modular and reusable
  • Rich API
    Qt includes up to 500 + C S++
  • A large number of development documents
    Network/XML/Open GL/Database/webkit/

1.2 QT application scope

KDE
ava
Google earth
0pera browser
Skype Internet phone
QCad
Adobe Photoshop Album
CGAL computational geometry Library
...

1.3 development environment

  • Visualization tools: Qt Designer, Qt Assistant Qt Linguist, Qt Creator
  • Command line program: update, release, qmake, uic, moc
  • Qt SDK development kit
  • Qvfd

1.4 QT software download and installation tutorial

1.4.1 QT software download

Official download address of QT version: http://download.qt.io/archive/qt/
Baidu cloud disk sharing download link: https://pan.baidu.com/s/1T-csDlc0B_C4EY7BYP-3mA Extraction code: zllk

1.4.2 QT software installation

To install the Linux Windows version, refer to the link: https://blog.csdn.net/qq_23473839/article/details/80523318​

-The detailed windows version installation tutorial is as follows:













QT software installation completed!

1.5 QT memory management

1.5.1. When new and delete are used, memory is allocated in the heap.
Heap memory space must be completely freed by delete to prevent memory leakage. Objects allocated on the heap can survive as long as they are not deleted.
The stack is automatically allocated and managed by the system. Local variables come from the stack area. As long as the stack area exceeds the scope, the data will be automatically recycled.

1.6. First QT procedure

1.6.1 create project




Three basic templates (QWidget/ QDialog/ QMainWindow)

Refer to the following links for the differences between the three basic templates: https://www.pianshen.com/article/20181654761/





Project creation completed!
1.6.2 first procedure

#Include < QPushButton > / / call the button header file

/*Select combination*/
QPushButton *bt;      	//Construct a pointer here

Refer to the RGB color comparison table: https://bj.96weixin.com/tools/rgb

this->setFixedSize(640, 480);           //Setup page
bt = new QPushButton("Sign in", this);     //Construct a button object
bt->setGeometry(300, 400, 100, 50);    //Set button coordinates
//bt->setStyleSheet("QPushButton{color:blue;};");               // Setting style; Set font color blue
//bt->setStyleSheet("QPushButton{background-color:blue;};");    // Setting style; Set the login box background to display blue
bt->setStyleSheet("QPushButton{background-color:#63B8FF;}; "); / / set style; set login. You can set the color blue according to the color code table

First program example code link:
Is the untitled1 folder: https://pan.baidu.com/s/1_sonChu0NyJhtJkmEsRSKQ
Extraction code: u4uy

1.7 print tracking


1.8 example of QT project

1.8.1. Basic template (QWidget/QDialog/QMainWindow)
1.8.2 process Declare the elements on the interface; 2. Construct the required controls; 3. Typesetting; 4. Front and rear hanging
1.8.3 details:
(1) Typesetting

  • Vertical QVBoxLayout
  • Horizontal QHBoxLayout
  • Grid (2D array) QGridLayout

(2) Signal and slot

  • public slots: declare slot functions
  • Connect (signal sending object, signal receiving object, slot function);

Programming example 1:
display a dialog box

#Include < QPushButton > / / call the button header file
#Include < qlineedit > / / line editor

/*Select combination*/
QPushButton *bt;    //Construct a pointer here
QLineEdit *le;      //Line editor

#include "QVBoxLayout" / / vertical class of typesetting

//1. Really apply for objects in the stacking area
bt = new QPushButton("Sign in", this);      //Construct a button object
le = new QLineEdit;                     //Construct a dialog box

//2. Make necessary typesetting
QVBoxLayout *vbox = new QVBoxLayout;    //Vertical layout
vbox->addWidget(le);                    //Row edit box
vbox->addWidget(bt);                    //Button
this->setLayout(vbox);                  //Display the current main interface

Programming example 1-2 code link:
In the untitled2 folder: https://pan.baidu.com/s/1_sonChu0NyJhtJkmEsRSKQ
Extraction code: u4uy

Programming example 2:
Read: click the button and the computer will broadcast voice automatically

#Include < QPushButton > / / call the button header file
#Include < qlineedit > / / line editor
#Include < qdebug > / / print trace
#Include < qtexttospeech > / / speaking class; You need to add a language conversion class in pro: texttospeech

/*read*/
public slots: //Declare this function as a slot function (which can be excited by a signal); QT c + + syntax
void xxx()
{
    x->say("Hello");                //Speaking class, language output Hello
    qDebug() << "xxxxxxxxxxxx";     //Display and print XXXXXXXXXXX
}

/*Select combination*/
QPushButton *bt;    //Construct a pointer here
QLineEdit *le;      //Line editor

QTextToSpeech *x;   //Talking class

QT       += core gui texttospeech

#include "QVBoxLayout" / / vertical class of typesetting

//1. Really apply for objects in the stacking area
bt = new QPushButton("read", this);      //Construct a button object
le = new QLineEdit;                     //Construct a dialog box

 x = new QTextToSpeech;                  //Construct a speaking class

//2. Make necessary typesetting
QVBoxLayout *vbox = new QVBoxLayout;    //Vertical layout
vbox->addWidget(le);                    //Row edit box
vbox->addWidget(bt);                    //Button
this->setLayout(vbox);                  //Display the current main interface

//3. Front and rear console hooking (connecting signal and cable)
connect(bt, SIGNAL(clicked(bool)), this, SLOT(xxx()));      //Signal of button connection; If the button is pressed, xxx below should click execute

x->say(le->text());             //Speaking class, edit the text in the box

Programming example 1-2 code link:
In the untitled2 folder: https://pan.baidu.com/s/1_sonChu0NyJhtJkmEsRSKQ ​
Extraction code: u4uy

Programming example 3:
Horizontal layout | grid layout

#Include < qhboxlayout > / / layout horizontal layout

QHBoxLayout *hbox = new QHBoxLayout;     //Horizontal layout
hbox->addWidget(le);                     //Row edit box
hbox->addWidget(bt);                     //Button
this->setLayout(hbox);                   //Display the current main interface

#Include < qgridlayout > / / typesetting grid layout

QGridLayout *gbox = new QGridLayout;      //Grid layout
gbox->addWidget(le, 0, 0, 1, 2);          //Row edit box
gbox->addWidget(bt, 1, 1, 1, 1);          //Button
setLayout(gbox);

Programming example 3 code link:
In the untitled3 folder: https://pan.baidu.com/s/1_sonChu0NyJhtJkmEsRSKQ ​
Extraction code: u4uy

Programming display: multiple button control



//Extract the signaled object
QPushButton *xbt = static_cast<QPushButton *>(sender());
qDebug()<<xbt->text();

Programming example: multiple button control links:
In the untitled3 folder: https://pan.baidu.com/s/1_sonChu0NyJhtJkmEsRSKQ ​
Extraction code: u4uy

1.9 QT graphic programming

1.9.1 establishment of the project:







1.9.2 project realization:


QT       += core gui texttospeech

#Include < qtexttospeech > / / speaking class; You need to add a language conversion class in pro: texttospeech

QTextToSpeech *x;   		//Talking class

x = new QTextToSpeech;      //Construct a speaking class

x->say("Have you eaten");         //Say a word


x->say(ui->lineEdit->text());         //Text voice broadcast in extraction box

QT graphic programming example code link:
In the untitled4 folder: https://pan.baidu.com/s/1_sonChu0NyJhtJkmEsRSKQ ​
Extraction code: u4uy

1.10 signal and slot

1.10.1 signal and slot

  • Signal and slot mechanism is a main feature of Qt, which is the most different part of Qt from other toolkits
  • A callback is a function pointer that is called when an event occurs, and any function can be scheduled as a callback
  • Signals and slots are more dynamic
  • Qt uses signal and slot to realize communication between object components.

1.10.2 signal and slot:

  • When the signal is transmitted, the QT code will call back the slot function connected to it
  • Signals will be processed by meta objects, and moc will be automatically translated into C + + code
  • The declaration of the signal is not in the cpp file, but in the header file

1.10.3 signal and slot function

  • Slot function is an ordinary C + + member function, which can be called normally
  • The slot function can have a return value or no return value
  • There are three access permissions for slot functions: public slots, private slots and protected
    slots. The access rights of the slot function determine who can be associated with it

1.10.4. Signal and slot: connection

1.10.5 signal and slot: send signal

  • Signal}- is usually sent by Qt during event processing. If the program needs to trigger the signa l itself, use emit.
  • The usage syntax is as follows
    emit signal

1.10.6 signal and slot: cancel connection

1.11. QT realizes the desired calculator

1.12. Extract QT files separately and use them freely








QT loader + file name

windeployqt.exe untitled4.exe

2, Window parts

2.1 introduction to common classes

2.2 dialog box

2.3 built in parts of QT

2.3.1 typesetting

2.3.2 common buttons
Normal button






Tool button








radio button




Check button




lamda expression (nameless function)

Check button - determine whether you click or not


2.3.3. Input parts

2.3.4 output components

2.3.5 QT assembly parts

2.3.6 containers

3, Main window

3.1. Basic widget (QWidget)

3.2. Layout manager

3.3. Qmain window

3.4 menu

3.5 tool bar

3.6 status bar

3.7 action

4, Event and graphics system

4.1 event handling

4.2 2D drawing foundation

4.3 coordinate system

4.4,Graphics View

5, File processing

5.1,QFiIe

5.2 reading and writing mode

5.3 text flow

5.4 data flow

5.5 temporary documents

5.6 contents

5.7 document information

5.8 document monitoring

6, Thread synchronization and mutual exclusion

6.1 thread QThread

6.2 thread synchronization and mutual exclusion

6.2.1 MUtex, QMutexLocker
6.2.2 read write lock QReadWriteLock
6.2.3 condition variable QWait Condition
6.2.4 SEmaphore

7, Process to process communication

7.1 Qprocess

7.2. Shared memory

7.2.1 Qt network programming
7.2.2 streaming socket
7.2.3 packet socket

7.3 Qt database programming

The course content is constantly updated

Keywords: C C++ Linux Qt AI

Added by aladin13 on Mon, 03 Jan 2022 05:57:34 +0200