Qt Programming (1)-Understanding

0. Comparison between Qt and MFC

During the interview, you may ask why QT is used instead of MFC to develop C++. After consulting some information, I summarized the following points. You can answer this in the interview:

Let’s talk about QT first:

  1. Cross-platform, it can be developed on multiple platforms such as Windows, Linux, and Unix.
  2. QT's GUI development is better than MFC, and the QT interface library supports CSS, making the interface design more convenient and beautiful.
  3. The object-oriented features are more obvious than MFC. It maintains excellent consistency in naming, inheritance, class organization, etc., and the code is more elegant to write.
  4. MFC has not developed much in recent years. QT has been updated and its functions are becoming more and more powerful.

Let’s talk about MFC:

  1. MFC mainly encapsulates the Windows API, so it can only be used on the Windows platform, and its status under the Windows platform is unquestionable.
  2. MFC runs programs more efficiently than QT.
  3. MFC's library is more comprehensive than QT's.

Personal summary:

  1. At this stage, MFC still has a large number of users. In recent years, QT has not been able to shake MFC's status for the time being, but it should take a longer-term view.
  2. Both QT and MFC are just development tools for programming. The most important thing about a program is the architecture, followed by the algorithm, and finally the interface implementation. You should focus more on the basics: C/C++ features, data structures and algorithms, etc.

1. The meaning of Qt

1. Introduction to Qt:

Qt is a cross-platform C++ graphical user interface application framework. It provides all the features a graphical interface requires for successful in-house developers. Object-oriented, easily extensible, and allows true component programming

2. Qt development:

  • It was first developed by Fantastic Technology Company in 1991.
  • It entered the commercial field in 1996 (KDE project founded by Matthias Ettrich).
  • Qt was acquired by Nokia in 2008. Became a programming language owned by Nokia.
  • In 2012, Qt was acquired by Digia.
  • In 2014, the cross-platform integrated development environment Qt Creator 3.1.0 was released. In the same year, the official version 5.3 was released, supporting current mainstream platforms: iOS, Android, WP (Windows phone has been closed) and other mobile platforms.

3. Platform:

  • Windows:XP,Vista,win7,win8,win10
  • Unix/Linux: Ubuntu (LTS version recommended)
  • Mac OSX
  • Embedded: Embedded Linux platform with framebuffer support, Windows CE

4. Qt version:

  • Commercial version: For commercial software development, it provides traditional commercial software distribution, and provides free upgrades and technical support services within a commercial limited period.
  • Open source version: Source code software designed for freedom of development, providing the same functions as commercial products, free under the GUN General Public License

5,Qt Creator:

It is a cross-platform development environment for Qt development. It has two major benefits: 1. It provides the first integrated development environment (IDE) designed to support cross-platform development and ensures that developers who are exposed to the Qt framework for the first time can Get up and running quickly. 2. Even if you do not develop Qt applications, Qt Creator is an easy-to-use and powerful IDE.

6. Qt advantages:

  • 1. Cross-platform, supports almost all platforms
  • 2. The interface is simple and easy to use. Learning the Qt framework is of reference for learning other frameworks.
  • 3. Object-oriented, highly modular, good reusability, easy to develop
  • 4. Rich API
  • 5. A large number of development documents
  • 6. Embedded development is possible

2. Installation of Qt5

1. Download Qt5

  • 1. Qt offline download address download address
  • 2. Select the archive project -> qt -> select the version (I chose 5.12.12. Large companies have a lot of technical accumulation due to historical baggage, and the version update span is long and prone to problems) -> Select the development environment according to the platform. Here I am. Using Windows
    Insert image description here
    Insert image description here
  • 3. After the download is completed, if you have a Qt account, you can install it directly without paying attention to the network. If you do not have a Qt account, you can disconnect from the Internet. This way you can skip the account login and select a location for installation.
    Insert image description here
  • 4. Select installation items:
    • Under the Windows system, it will be more convenient to choose the compilation tool developed by Windows. Click next to configure the installation components and select the MSVC 32-bit version and the 64-bit version;
    • Sources is the source code and it is recommended to check it. You can understand the principles of Qt by looking at the source code;
    • UWP has very few mobile terminal development and usage scenarios under Windows, and Windows-based mobile phones have almost withdrawn from the market.
    • Tools is a Debug debugging tool. If it is embedded, add a check mark to Strawberry Perl;
      Insert image description here
  • 5. After the Qt setting is completed, next, accept the agreement, and then wait for the installation. The installation time is different depending on the computer configuration.
  • 6. The installation is now complete.
    Insert image description here

2. Understanding the Qt Creator environment

Insert image description here

  • Qt main interface has
    • Welcome: Projects, Samples, Tutorials, Marketplace
    • Edit: Project debugging uses
    • Design: Project debugging use
    • Debug: used for project debugging
    • Project: Project structure presentation
    • Help: The help provides many library files used in the Qt development process.

3. Introduction to Qt project

1. New project

  • 1. In the welcome module -> New-> create a new project, which includes application, library, other projects (QT plug-in), empty project (Non-Qt Project), and import library (import Project)
    Insert image description here

  • 2. Select Build System. Qt uses the qmake compilation system (supported after 5.0). If the project needs to migrate the environment, such as Linux, etc., select CMake.
    Insert image description here

  • 3. Base Class->Select QMainWindow (main window), QWidget (non-modal view), QDialog (dialog box) in Detail
    Insert image description here

  • 4. Translation file, language: select the language according to the project requirements, here select Chinese mainland
    Insert image description here

  • 5. Kits tool set is selected according to needs. I choose the 32-bit version of 2017 here.
    Insert image description here

    • MSVC: It is a compilation environment developed by Microsoft. When we use the Windows operating system for development, there are fewer chances of bugs using Microsoft's compilation environment. We all have 32-bit operating systems and APPs, and the maximum addressing range is only 4G. Here we do a simple demonstration. They are basically small apps and do not require 4G memory. We choose the 32-bit MSVC compilation environment.
  • 6. If you need to support mobile devices in version control, just add the corresponding configuration. For the sake of simplicity, we do not need to add version control here:
    Insert image description here

  • Project management: Used in multi-person collaborative development projects, there are also functions required for version iteration and rollback. We will talk about it later, just click to complete.

2. Introduction to project files

  • 1. After the project is built, the qt development environment helps us create all the primary files.
    Insert image description here
  • 2. After the creation is completed, we can run it to see the effect:
    Insert image description here
  • 3. Main.cpp introduction
    Insert image description here
    • main function, the entry point of the application
    • QApplication: The header file of the Qt application, which is required for every interface program developed by Qt.
    • In the above figure, the main function is the main function with parameters.
    • QApplication a(argc, argv); Create an App object a of the Qt framework. Each app has only one a object.
    • QTranslator: translator
    • MainWindow: Create a custom window object w
    • w.show(): The created window object w can be displayed by calling the show() function.
    • a.exec(): app object a, calls the exec() function to receive signals in a loop, process the signals, and exit the app if the signal is closed.
  • 4. Mainwindow introduction
    Insert image description here
    • Q_OBJECT: Signals and slots are the basis of Qt development. They can connect two unrelated objects together. Slots are the same as ordinary C++ functions. Only after they are connected to signals, when a signal is sent, the slot will automatically When called, the signal and slot mechanisms in Qt can only be used if Q_OBJECT is added.
    • parent: a pointer to the parent class.

3. Debugger problem

  • Some friends may encounter debugger problems and find that the debugger cannot work. This is because the SDK of win10 does not have debugging tools installed by default and needs to be installed by yourself.
    Insert image description here

四,QPushButton

1. Manually create QPushButton

  • 1. Introduce the header file, #include
  • 2. Create the QPushButton object in the constructor and remember to delete the destructor.
  • 3. Set button properties:
    Insert image description here

2. Visually create QPushButton

  • Double-click the .ui desktop board to be created in the Edit->Forms file.
    Insert image description here
    The graphical interface is very convenient for creating and modifying buttons. What you see is what you need. In the lower right corner is the object's property setting box, where you can modify the data. In the visual interface You can see the effect of modifications in time.

3. Introduction to common functions of the property panel

Insert image description here

Some commonly used attribute parameters:

  • ObjectName: Object name, which cannot be modified in the property box, but can be modified by right-clicking the button Object > Modify Object Name.
  • Enbled: Whether to enable the button. If not enabled, the entire button will be gray and unclickable during runtime.
  • Geometry: geometric parameters, (220, 110) is the coordinate origin, 91x31 is the object width and height
  • Palette: color parameter, here directly inherits the color of the parent class
  • Font: font parameters
  • Cursor: Cursor refers to the cursor style when the mouse moves to the button. Here it is still an arrow.
  • styleSheet: adjust color, background, font color

5. Qt Help Document: Use of Qt Assistant

1. Open Qt Assistant

Click on the system, search for assistant, and see:
Insert image description here
Insert image description here
We can learn Qt through the help document. The assistant has an introduction to all classes, controls, and methods, which is very helpful for us to learn Qt.
For example, index to search QPushButton:
Insert image description here

2. Briefly introduce the content of the assistant document

Insert image description here

  • qmake:QT+=widgets : When compiling, the widgets module must be added to the .pro file, but this is a basic module and has been added by default. If we need the TCP network and need to use the QTcpSocket class, then we need Add the network module to the .pro file, otherwise the network cannot be used.
    Insert image description here
  • Inherits : QAbstractButton This tells the class that it inherits the QAbstractButton class.
  • InHerited By : QCommandLinkButton, a subclass of this class.
  • In addition, there are some public, protected, and private methods that provide us with rich interfaces when writing programs.

6. Qt object tree

1. What is an object tree?

  • QObject in Qt uses an object tree to organize and manage itself. For example, if you create a QObject and specify the parent object, it will add itself to the parent object's children() list, that is, the QList<QObject *> variable.
  • Every time we create a new object, we will assign it a parent class, for example: an object myObject, and this object will also be assigned a parent class, that is, QObject when it is constructed.

2. Benefits of object tree mode

  • The advantage is that when the parent object is destructed, the child object will also be destructed, which simplifies the memory recycling mechanism to a certain extent. Qt uses the object tree mode. When the parent object is destructed, the child object is automatically deleted. There is no need to write a bunch of code to release the object.
  • The system will release the memory of the entire structure based on this tree structure.
    Insert image description here

3. Problems with object trees

  • Automatic destruction involves C++ syntax issues. If the sub-object is automatically released due to the system mechanism, the sub-object may be released twice, excessive destruction, and the object on the heap will crash when the stack is released.
    Insert image description here
  • After adjusting the stack release order, the crash problem can be solved:
    Insert image description here

7. Signals and slots in Qt

Similar to, send and receive.

1. Signal&Slot

  • Signal & Slot (Signal & Slot) is the foundation of Qt and a major innovation of Qt. Because of the programming mechanism of signal and slot, it becomes more intuitive and simple to handle the interaction of various interface components in Qt. It can make app developers bind objects that don’t understand each other together

2. Signal

  • Signals are events that are sent under specific circumstances. For example, the most common signal for PushButton is the clicked() signal sent when the mouse is clicked. The most common signal for a ComboBox is the CurrentIndexChanged() signal sent when the selected list item changes.
  • GUI programming is mainly about responding to the signals of each component of the interface. You only need to know which signals to send under what circumstances, and respond and process these signals reasonably.

3. Slot

  • A slot is a response function to a signal. Like a general C++ function, it can be defined in public, private, or protect of a class, can have any parameters, and can also be called directly.
  • The difference between slot functions and general functions is that slot functions can be associated with a signal. When the signal is sent, the associated slot function is automatically executed.
  • The association between signals and slots is implemented using the QObject.connect() function. The basic format is:
QObject::connect(sender, SIGNAL(signal()), receiver, SLOT(slot()));
参数1:信号的发送者
参数2:发送的信号
参数3:信号的接收者
参数4:处理函数(槽函数)

4. Signal and slot example QPushButton

  • 1. We open the Qt Assistant, search for QPushButton, and find that there is no signal, only a public slot.
    Insert image description here
  • Find the parent class of QPushButton, QAbstractButton, and find signals
    Insert image description here
  • 2. You can see that there are 4 signals inside
    Insert image description here
  • 3. Regarding the response slot function, it is usually inside the window. For example, the MainWindow class is a subclass of QMainWindow:
    Insert image description here
  • 4. Find the function from the public slot
    Insert image description here
    Insert image description here
  • 5,Pay attention to the use of click and clicked. Clicked is a signal and click is a slot function., examples are as follows:
    Insert image description here

Guess you like

Origin blog.csdn.net/MOON_YZM/article/details/130544390