Qt development entry-level nanny-level tutorial (including installation and environment variable configuration)

1. Qt Overview

1.1 What is Qt

Qt is a cross-platform C++ graphical user interface application framework. It provides application developers with all the functionality they need to build graphical interfaces. It is fully object-oriented, easily extensible, and allows true component programming.

1.2 Development History of Qt

Qt was first developed by Finnish QQ Technology in 1991

Entering the commercial field in 1996, it is also the basis for the currently popular Linux desktop environment KDE

In 2008, Trolltech was acquired by Nokia, and Qt became the programming foundation of Nokia.

In 2012, Qt was acquired by Digia (a Finnish software company)

The cross-platform integrated development environment Qt Creator 3.1.0 was released in April 2014, and the official version of Qt 5.3 was distributed on May 20 of the same year. So far, Qt has fully supported various platforms such as iOS, Android, and WP.

In 2016, QT 5.7 was released, adding many new features, including Qt for WebAssembly.

In 2019, the Qt team announced the release of the latest version 5.13.

In 2020, QT 6.0 was released. This is a brand new version. Compared with previous versions, QT 6.0 is more modern, efficient and secure.

1.3 Advantages of Qt

Cross-platform, supports almost all platforms

Windows – XP、Vista、Win7、Win8、Win2008、Win10

Uinux/X11 – Linux, Sun Solaris, HP-UX, Compaq Tru64 UNIX, IBM AIX, SGI IRIX, FreeBSD, BSD/OS, and many other X11 platforms

Macintosh – Mac OS X

Embedded - Embedded Linux platform with framebuffer support, Windows CE

The interface is simple and easy to use. Learning the QT framework is a reference for learning other frameworks.

Simplified the memory recycling mechanism to a certain extent

The development efficiency is high and applications can be built quickly.

There is a good community atmosphere and the market share is slowly rising.

Embedded development is possible.

1.4 Qt version

Qt is released in different versions, divided into commercial version and open source version

Business version

Providing development for commercial software, they provide traditional commercial software distributions, and provide free upgrades and technical support services during the commercial validity period.

Open source LGPL version

Open source software designed for developing your own, it provides the same functionality as the commercial version, and it is free under the GNU General Public License.

This is the version I am currently using. You can download the latest version from the official website:

http://download.qt.io/archive/qt/

1.5 Success Stories

Linux desktop environment KDE (K Desktop Environment)

WPS Office office software

Skype VoIP

Google Earth Google Earth

VLC multimedia player

VirtualBox virtual machine software

etc.

1.6 Recent employment trends

 

2. Qt installation package download

2.1 Qt Chinese official website

 https://www.qt.io/zh-cn/

2.2 Download address

 http://download.qt.io/archive/qt/

What is MinGW?

Minimalist GNU for Windows

It is a collection of freely available and freely redistributable Windows-specific header files and import libraries using the GNU toolset, allowing you to generate native Windows programs on GNU/Linux and Windows platforms without the need for a third-party C runtime (C Runtime) Library.

3. Qt installation

During the learning process, we use Qt5.14.0 version by default, and the latest version is 6.4.0.

Note: Qt5.14.2 is the last version that provides binary installation packages, and subsequent versions need to be installed online.

3.1 Offline installation

Offline installation requires downloading versions below Qt5.14.2 (including 5.14.2). The following describes the installation process of Qt5.14.0.

 

 

 

 

Just follow the wizard prompts for subsequent installation.

3.2 Online installation

Qt5.14.2 and later versions need to be installed online, official website online download address:Index of /official_releases/online_installers

The installation process is as follows:

After the download is complete, double-click to open the running program.

 

 

Just follow the wizard prompts for subsequent installation.

3.2.1 Installation error

source problem

Temporary Alibaba Cloud source problem.

Solution:

Use Fiddler to switch installation sources

I don't have the speed to install Qt online, so I searched and found a method and shared it. Install Fiddler software on your computer. This is an Http proxy software. You can use this software to redirect the URL accessed by qt online installation to the domestic source of qt, so the speed is very fast.

Fiddler download address: https://www.telerik.com/fiddler

How to operate:

Install Fiddler on your computer. After opening it, select the menu Rules, Customize Rules..., and Fiddler ScriptEditor will open.

Fiddler ScriptEditor selects the menu Go, to OnBeforeRequest, and it will jump to the beginning of the definition of a function, and then pull down all the way to the end of the curly braces of the function.

If you want to use the source from USTC, insert the following content before the end of the function:

if (oSession.HostnameIs("download.qt.io") && oSession.PathAndQuery.StartsWith("/online/"))
{
    oSession.hostname = "mirrors.ustc.edu.cn";
    oSession.PathAndQuery = oSession.PathAndQuery.Replace("/online/", "/qtproject/online/");
}

The meaning of this code is that if the access URL is download.qt.io/online/, it will be converted to the access URL mirrors.ustc.edu.cn/qtproject/online/.

If you use the Tsinghua University source, replace the above code with:

if (oSession.HostnameIs("download.qt.io") && oSession.PathAndQuery.StartsWith("/online/"))
{
    oSession.hostname = "mirrors.tuna.tsinghua.edu.cn";
    oSession.PathAndQuery = oSession.PathAndQuery.Replace("/online/", "/qt/online/");
}

Just add it and save it later.

Open the qt online installer and install according to the normal process. In Fiddler's main interface, you can see that all access to download.qt.io is redirected to the source you set. After the installation is complete, you can close the Fiddler software.

Note: Because the Qt online installer uses the system's default agent when it is started, if you need the Qt installer to use the set source, you must first open the Fiddler software, and then open the Qt installer. Fiddler only needs to be set up once and closed when not in use.

4. Qt environment variable configuration

Assume that the QT installation directory is: C:\Qt\Qt5.14.0

Just add the directory: C:\Qt\Qt5.14.0\5.14.0\mingw73_32\bin to the system environment variables.

4.1 win7

 

Add the two paths found above to the Path environment variable, and the path and path are directly separated by a semicolon (;)

4.2 win10

Just create a new path and add the two paths found above.

4.3 The purpose of configuring environment variables

The compiled qt executable program can be opened at any location on the current computer, otherwise it will prompt an error that the relevant dynamic library cannot be found.

4.4 Test whether the environment variables are configured successfully

Open a command line window under Windows: windows key + R

Then press the Enter key to pop up the window

Enter in the command line window (any path is acceptable):

qmake –v

If you can see the version information, the configuration is successful. If there is no corresponding information output, restart the CMD computer and the environment variable settings will take effect.

5. A simplest Qt application

5.1 In main function

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

Analysis:

The class header files provided by the Qt system do not have the .h suffix

A Qt class corresponds to a header file, and the class name is consistent with the header file name.

QApplication application class

Manage the control flow and main settings of a graphical user interface application.

It is the life of Qt. If a program wants to ensure that it always runs, it must have at least one loop. This is the Qt main message loop, in which all event message processing and dispatching from the window system and other resources are completed. It also handles application initialization and termination, and provides session management.

For any graphical user interface application using Qt, there is exactly one QApplication object, no matter how many windows the application has at the same time.

a.exec() The program enters the message loop and waits for a response to user input. Here main() transfers control to Qt, Qt completes the event processing, and the value of exec() will be returned when the application exits. In exec(), Qt accepts and processes user and system events and passes them to the appropriate widgets.

5.2 Class header files

#include <QWidget>

class MyWidget : public QWidget
{
    //引入Qt信号和槽机制的一个宏
    Q_OBJECT

public:
//构造函数中parent是指父窗口
//如果parent是0,那么窗口就是一个顶层的窗口
    MyWidget (QWidget *parent = 0);
    ~ MyWidget ();
};

5.3 .pro file

.pro is the project file, which is the configuration file automatically generated by qmake for producing makefiles. Similar to .sln and vsproj files in VS

The following is an example of a .pro file:

#引入Qt的模块,core gui
QT       += core gui
#如果qt版本大于4,那么引入widgets模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#生成最终文件的文件名,可执行文件exe
TARGET = 01_MyWidget
#项目类型,生成什么类型的文件,可执行程序还是库文件
TEMPLATE = app
#要编译的源文件列表
SOURCES += \
        main.cpp \
        mywidget.cpp
#要编译的头文件列表
HEADERS += \
        mywidget.h

Rules for .pro files:

Comment

Start with "#" and end with this line.

Module introduction

QT += module name, indicating which modules of Qt are introduced into the current project.

The meaning of introducing a module is simply understood as introducing the C/C++ header file search path. If the header file is used without introducing the corresponding module, an error will be reported saying that the header file cannot be found. Of course, it is better not to introduce unnecessary modules, because introducing modules is not just as simple as introducing the header file search path, but also includes a series of operations such as introducing connected libraries, which will make the program bloated.

Please refer to the appendix for detailed Qt modules.

Template variables tell qmake what kind of makefile to generate for this application. Here are the options available: TEMPLATE

app

lib

vcapp

vclib

subdirs

Specify the generated application name:

TARGET

Header files included in the project

HEADERS

.ui design files included in the project

FORMS

Source files included in the project

SOURCES += sources/main.cpp sources

Resource files included in the project

RESOURCES

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

The meaning of this statement is that if QT_MAJOR_VERSION is greater than 4 (that is, the currently used Qt5 and higher versions), the widgets module needs to be added. If the project only needs to support Qt5, you can also directly add the sentence "QT += widgets". However, in order to maintain code compatibility, it is best to write according to the statements generated by QtCreator.

Configuration information

CONFIG is used to tell qmake about the application's configuration information.

CONFIG += c++11

"+=" is used here because we are adding our configuration options to any ones that already exist. This is safer than using "=", which replaces all options that have been specified.

5.4 Naming convention

5.4.1 Class name

The first letter of a word is capitalized, and words are connected directly without connecting characters.

MyClass,QPushButton
class MainWindow

5.4.2 Built-in types in Qt

The header file and class name have the same name.

#include <QString>
        QSring str;
        
        #include <QWidget>
        QWidget w;

5.4.3 Function name, variable name

The first letter is lowercase, and the first letter of each subsequent word is capitalized. Words are connected directly without connecting characters.

void connectTheSignal();

The member variable setting function of the class uses set+member variable name, and the function to obtain the member variable directly uses the member variable name (if it is a bool type, some terms indicating the state may be used, such as isVisilble, hasFocus):

//普通成员变量设置和获取
        void setText(QString text);
        QString text()const;
        //bool的成员变量设置和获取
        void setEnabled(bool enabled);
        bool isEnabled()const;

5.5 Common shortcut keys for QtCreator

Run ctrl +R

Compile ctrl +B

Help document F1, click F1 twice to jump to the help interface

Jump to symbol definition F2 or ctrl + mouse click

Comment ctrl+/

Font zoom ctrl + mouse wheel

Move the entire line of code ctrl + shift + ↑ or ↓

Auto-align ctrl + i

Jump between .h and .cpp files with the same name F4

[Article Benefits] Qt development learning information package, interview questions from major manufacturers, technical videos and learning roadmaps, including (Qt C++ basics, database programming, Qt project practice, Qt framework, QML, Opencv, qt threads, etc.) if needed You can get it at Penguin Skirt 937552610~

Guess you like

Origin blog.csdn.net/hw5230/article/details/134716058