当前位置:网站首页>1. Getting started with QT

1. Getting started with QT

2022-07-04 07:48:00 master cat

1. Qt summary

1.1 Qt Characteristics

  • It's a cross platform C++ Application development framework
    • It has the excellent characteristics of being short and fast : Less investment 、 Cycle is short 、 Be quick 、 High benefit
    • Almost all platforms are supported , It can be used for desktop program development and embedded development
    • It has its own event handling mechanism
  • Qt It's a standard. c++ An extension of , c++ The grammar of is Qt Both of them support
    • Good encapsulation mechanism makes Qt It's very modular , Good reusability , You can get started quickly .
    • Qt There is provided a type called signals/slots Instead of callback, This makes the components It's very easy to work together .
  • Widely used in development GUI Program , It can also be used to develop non GUI Program .
  • graphical user interface
  • Have a lot of API
    • Qt Including up to 250 More than one C++ class
    • You can handle regular expressions .
  • Support 2D/3D Graphic rendering , Support OpenGL
  • Qt It provides the program ape with very detailed official documents
  • Support XML,Json
  • Bottom modularization of framework , Users can choose the corresponding module to use according to their needs

1.2 Qt Module in

Qt A large number of classes in the class library are divided into various modules according to their functions , These modules are divided into the following categories :

  • Qt Basic modules (Qt Essentials): Provides Qt Basic functions on all platforms .
  • Qt Add on modules (Qt Add-Ons): Modules that provide added value to achieve some specific functions .
  • Value added module (Value-AddModules): Separately released modules or tools that provide additional value .
  • Technology preview module (Technology Preview Modules): Some are in the development stage , But it can be used as a module for technical preview .
  • Qt Tools (Qt Tools): Some tools to help application development .

Qt Official website or help documents “All Modules” The information of all these modules can be viewed on the page . Here's the official response to Qt Description of basic modules . If you are interested in other modules, you can check them by yourself .

modular describe
Qt CoreQt The core of class library , All other modules depend on this module
Qt GUI Design GUI The basic class of the interface , Include OpenGL
Qt Multimedia Audio 、 video 、 Class of camera and broadcast functions
Qt Multimedia Widgets Interface component class to realize multimedia function
Qt Network Classes that make network programming simpler and lighter
Qt QML be used for QML and JavaScript The class of language
Qt Quick A declarative framework for building dynamic applications with custom user interfaces
Qt Quick Controls Create a desktop style user interface , be based on Qt Quick User interface controls
Qt Quick Dialogs be used for Qt Quick Type of system dialog
Qt Quick Layouts be used for Qt Quick 2 Layout items of interface elements
Qt SQL Use SQL Classes for database operations
Qt Test Classes for unit testing applications and Libraries
Qt Widgets Used to build GUI Interface C++ Graphics component class

1.3 Qt Case study

  • VirtualBox: Virtual machine software .
  • VLC Multimedia player : One is small 、 Powerful open source media player .
  • YY voice : also called “ Crooked voice ”, It is a free software that can carry out online multi person voice chat and voice conference .
  • Migu music : Migu music is a genuine music player made by China Mobile
  • WPS Office: Jinshan company (Kingsoft) Office software produced , With Microsoft Office Good compatibility , Personal edition is free .
  • Skype: A large number of users based on P2P Of VOIP Chatting software .

2. install

Qt Download address :
https://download.qt.io/archive/qt/
 This tutorial is based on Window platform  Qt 5.14.2  Explain to you how to install and configure .

2. Installation steps

You can go to B Station looking for video

3. Create the first one Qt project

3.1 Create project

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OFP1Uo53-1656382609082)(assets/image-20200909122848368.png)]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-JQyqC7mt-1656382609083)(assets/image-20200909123014167.png)]

  • The project name can be specified according to the needs
  • When specifying the storage path of the project , The path cannot contain Chinese , Cannot contain Chinese , Cannot contain Chinese

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-5QMjovfI-1656382609084)(assets/image-20200909123314313.png)]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-twxQoDq2-1656382609085)(assets/image-20200909123356763.png)]

  • The compilation suite is used for the compilation of project files , If more than one compilation suite is installed , Just choose one of them here

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-xb1Gqb8I-1656382609086)(assets/image-20200909123543447.png)]

3.2 Project documents .pro

#  In the project file ,  Annotations need to use   Well No (#)
#  Which low-level modules need to be loaded when compiling the project 
QT       += core gui 

#  If at present Qt The version is greater than 4,  An additional module will be added : widgets
# Qt 5 Chinese vs gui The module is split ,  take  widgets  Independent 
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
   
#  Use c++11 New characteristics 
CONFIG += c++11	

# If an obsolete function is called in the project ,  There will be a warning prompt when the project is compiled  
DEFINES += QT_DEPRECATED_WARNINGS

#  Source files in the project 
SOURCES += \
        main.cpp \
        mainwindow.cpp
        
#  Header file in project 
HEADERS += \
        mainwindow.h
        
#  Window interface file in the project 
FORMS += \
        mainwindow.ui

3.3 Other documents

  • main.cpp

    #include "mainwindow.h"		//  Generated window class header file 
    #include <QApplication>		//  Application class header file 
    
    int main(int argc, char *argv[])
    {
        //  Create application objects ,  In a Qt There is only one instance object in the project 
        //  The role of classes :  Detect triggered events ,  Loop events and handle 
        QApplication a(argc, argv);
        //  Create a window class object 
        MainWindow w;
        //  Display window 
        w.show();
        //  The application object starts the event loop ,  Ensure that the application does not exit 
        return a.exec();
    }
    
  • mainwindow.ui

    <!--  Double click this file to see a window interface ,  If you open it with a text editor, you will see a XML File format  -->
    <!--  It doesn't matter if you don't understand this format ,  We don't need to operate this file in this mode ,  Here is just the essence of this document  -->
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget"/>
      <widget class="QMenuBar" name="menubar"/>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
  • mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>		// Qt Standard window class header file 
    
    QT_BEGIN_NAMESPACE
    // mainwindow.ui  There is also a class called  MainWindow,  Put this class into the namespace  Ui  in 
    namespace Ui { class MainWindow; }	
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT	//  This macro is to be able to use Qt Signal slot mechanism in 
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;		//  Define that the pointer points to the window  UI  object 
    };
    #endif // MAINWINDOW_H
    
  • mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)	//  be based on mainwindow.ui Create an instance object 
    {
        //  take  mainwindow.ui  Instance object and   Associate objects of the current class 
        //  In this way, an object of the same name is associated ,  Two in one 
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

4. Qt Window class in

We're passing Qt Wizard window the penultimate step in the project process of a window based application lets us choose the base class of the first window created following the project , There are three options in the drop-down menu , Respectively : QMainWindowQDialogQWidget Here's the picture :

4.1 Basic window class

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mwLEhQBh-1656382609087)(assets/image-20200909165056959.png)]

  • Common window classes are 3 individual
    • Creating Qt At the window , You need to make your own window class inherit one of the above three window classes
  • QWidget
    • The base class of all window classes
    • Qt Control in ( Button , Input box , Radio buttons …) Also belongs to window , Base classes are all QWidget
    • It can be embedded in other windows : No borders
    • It can be displayed separately without embedding : Independent window , There's a border
  • QDialog
    • Dialog class , Later chapters will introduce this window in detail
    • Cannot be embedded in other windows
  • QMainWindow
    • There is a toolbar , status bar , menu bar , Later chapters will introduce this window in detail
    • Cannot be embedded in other windows

4.2 Window display

  • Embedded window

    • Attached to a large window , As part of the big window
    • The large window is the parent window of the embedded window
    • When the parent window is displayed , The embedded window is also displayed
  • Do not embed windows

    • Such windows have borders , There's a title bar

    • You need to call a function to display

      // QWidget Is the base class for all window classes ,  Call this provided  show()  Method can display any window 
      //  Non modal display 
      void QWidget::show();	//  Displays the current window and its child windows 
      
      //  Modeless display of dialog window :  Or call show()  Method 
      //  Modal display of dialog window 
      [virtual slot] int QDialog::exec();
      

5. Qt The coordinate system of

5.1 The coordinate origin of the window

Qt The coordinate origin of is in the upper left corner of the window

  • x Axial right increment
  • y It's going down the axis

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-jrEmf7xl-1656382609087)(assets/1.png)]

5.2 The relative coordinates of the window

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-H842PXmc-1656382609088)(assets/2.png)]

  • stay Qt There may be several controls in a window of , This control is nested

    • A Window contains B window , B Window contains C window
  • Every window has a coordinate origin , In the upper left corner

    • The position of the child window is determined based on the coordinate system of the parent window , That is to say, you can determine your position through the coordinate point in the upper left corner of the parent window
  • Qt The relative coordinates used when displaying in the window , Relative to your parent window

  • Move the child window to a certain position of the parent window

    //  The base class of all window classes : QWidget
    // QWidget For moving windows  API function 
    //  Parameters  x, y Is the point in the upper left corner of the window to be moved ,  The upper left corner of the window moves to this coordinate point 
    void move(int x, int y);
    void move(const QPoint &);
    
    

6. Qt Memory recovery mechanism

stay Qt When you create an object in the Parent Object pointer ( You can view the constructor of the class ), Let's explain this parent What is it for .

QObject It's organized in the form of an object tree . When you create a QObject Object time , Will see QObject The constructor of receives an QObject Pointer as parameter , This parameter is parent, That is, the parent object pointer . This is equivalent to , Creating QObject Object time , You can provide a parent object , We created this QObject Object is automatically added to the children() list . When the parent object is destructed , All objects in this list are also destructed .( Be careful , The parent object here is not a parent class in the sense of inheritance !

QWidget Is the parent of all components that can be displayed on the screen .QWidget Inherited from QObject, So it also inherits this object tree relationship . A child automatically becomes a child of the parent component . therefore , It will be displayed in the coordinate system of the parent component , Clipped by the boundaries of the parent component . for example , When the user closes a dialog , The application removes it , that , We want the buttons that belong to this dialog 、 Icons and so on should be deleted together . That's what it is , Because these are all subcomponents of the dialog box .

Qt The concept of object tree is introduced , It solves the memory problem to a certain extent .

  • When one QObject When objects are created on the heap ,Qt An object tree will be created for it at the same time . however , The order of objects in the object tree is undefined . It means , The order in which these objects are destroyed is also undefined .

  • In any object tree QObject object delete When , If the object has parent, It will be automatically transferred from parent Of children() Delete... From the list ; If you have children , Automatically delete Every child .Qt Promise not to QObject Will be delete two , This is determined by the order of deconstruction .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-VLASFyzR-1656382609089)(assets/image-20200909165237496.png)]

in summary , We can come to a conclusion : Qt There is a memory recycling mechanism in , But not all are new The object is automatically recycled , Only when the conditions are met can it be recycled , If you want to Qt Realize the automatic recycling of memory , There are two conditions that need to be met :

  • The object created must be QObject Subclasses of classes ( Indirect subclasses can also )

    • QObject Class has no parent , Qt A large number of classifications in are derived from this class

      • Qt Window classes and controls that are frequently used in are QObject Direct or indirect subclasses of
      • Other classes can be consulted by themselves Qt Help document
  • Created class object , You must specify who its parent object is , Generally, there are two operation modes :


  //  The way 1:  By constructor 
  // parent:  The parent object of the current window ,  Find  parent  Parameters can be 
  QWidget::QWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
  QTimer::QTimer(QObject *parent = nullptr);
  
  //  The way 2:  adopt setParent() Method 
  //  Suppose that this control does not specify a character object when it is constructed ,  You can call QWidget Of api Specify the parent window object 
  void QWidget::setParent(QWidget *parent);
  void QObject::setParent(QObject *parent);

ject Direct or indirect subclasses of
- Other classes can be consulted by themselves Qt Help document

  • Created class object , You must specify who its parent object is , Generally, there are two operation modes :

  //  The way 1:  By constructor 
  // parent:  The parent object of the current window ,  Find  parent  Parameters can be 
  QWidget::QWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
  QTimer::QTimer(QObject *parent = nullptr);
  
  //  The way 2:  adopt setParent() Method 
  //  Suppose that this control does not specify a character object when it is constructed ,  You can call QWidget Of api Specify the parent window object 
  void QWidget::setParent(QWidget *parent);
  void QObject::setParent(QObject *parent);

原网站

版权声明
本文为[master cat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040743054627.html