Cross-platform C++ Development Framework Qt Tutorial: Summary of Project Architecture Experience

Qt is currently the most advanced and complete cross-platform C++ development tool. It not only realizes one-time writing and runs indiscriminately on all platforms, but also provides almost all tools needed in the development process. Today, Qt has been used in more than 70 industries, thousands of companies, and supports millions of devices and applications.

Click to download the latest trial version of Qt

#### (1) General rules
1. Except for very small micro-demo-level projects, it is recommended to store code files in pri-category and different folders for other projects, which is convenient for unified management and search.
2. Classes with the same type of function are recommended to be put together. If there are too many code files in the directory, it is also recommended to split into multiple directories for storage.
3. For example, for projects with 3-5 interfaces, a unified form.pri will store these interfaces. When the project becomes larger, the interface may also need to be divided according to functions. For example, the form of the system configuration is placed in a directory. The log management form is placed in a directory.
4. Many common functions are used in multiple projects. You can consider encapsulating them into modules in the form of pri, commonly known as wheels. These wheels are constantly being improved. Multiple projects share the module. Once you encounter a bug fix, you only need to change one place.
5. If the project is larger or the project team personnel allocate different functions, you can consider the plug-in form. Generally, two plug-ins are used. One is the plug-in in the form of a common dynamic library, which must be placed with the main program; the other is Qt The plug-in of the mechanism is placed in the specified directory.

#### (2) Global configuration file The
global configuration file management class appconfig.h is used to read and write the configuration file of the corresponding project.
1. The format can be ini, xml, json, etc., small projects suggest ini, how convenient and how to come, it is equivalent to mapping the value of the configuration file to a global variable.
2. If there are many configuration items in the configuration file, it is recommended to store them in groups for easy searching instead of putting them all in one big group.
3. When reading the configuration file, it can be judged whether the configuration file exists, whether the configuration item is missing, etc., if there is a problem, the configuration file is regenerated to avoid malicious deletion of the configuration file and cause the program to run abnormally.
4. When reading the configuration file, you can fill in the default value (the second parameter of the value method of the qt configuration file class QSettings, set.value("Hardware", App::Hardware)) to avoid not being able to read the node initially As a result, the configuration item value does not meet the expected value type.
5. After reading the configuration file, you can re-judge whether the value of the configuration item meets the requirements, filter and correct the value to prevent artificially opening the configuration file and filling in the abnormal value after modification. For example, the timer interval is 0, and it needs to be corrected again. Set to a legal value.
6. The initial value with Chinese is wrapped with QString::fromUtf8, such as QString::fromUtf8("Administrator").
7. For configuration items with Chinese, set the configuration file code to utf-8, set.setIniCodec("utf-8").

#### (3) Global Variables The
global variable management class appdata.h is used to set all global variables used in the project.
1. For example, whether the current user/system is locked, etc., so that the variable can be used in any coding position for judgment processing.
2. You can put the navigation bar width and height, button size, icon size and other variables in the UI interface here, and set different values ​​by judging the resolution after the system is started.

#### (4) Global event transfer processing The
global event transfer processing class appevent.h is used to transfer various events across multiple UIs and multiple classes in the system.
1. This class must be a global singleton class, so that it can be used globally.
2. For example, the parent class of class a is b, and the parent class of class b is c. Now there is a signal to be sent to class d. When there is no event transfer processing, the method is to send a signal to b, and then b Send to c, and then to d. If the parent class has more nesting levels, the more complex it is, the more difficult it is to manage the code.
3.
4. The larger the project, the more necessary it will be to handle the incident, the code is clear, and the management is convenient.

#### (5) Global program initialization The
global program initialization class appinit.h is used to initialize some programs after startup.
1. Read the configuration file.
2. Set the global font.
3. To set the global style sheet, it is recommended to read the general style sheet first, and then add the content of the extra style sheet to the back and set it together.
4. Set the project code.
5. Set the translation file, you can load multiple, including the built-in qt_zh_CN.qm of qt, the user's own translation file, etc.
6. Initialize the random number seed.
7. Create the required directory in the new project to prevent the file cannot be saved to the directory without a directory.
8. Initialize the database, including opening the database and loading basic data such as user tables, device tables, etc.
9. The start log output class is used to start the log service.
10. The startup running time record class is used to record the start time and end time of each software run.
11. Associate global event filters to handle custom borderless UI dragging, global button processing, etc.

#### (6) Global general class
1. The debug log output class savelog.h is used to start the log service, which can output the log to a file or print it out on the network.
2. Run time recording class saveruntime.h is used to record the start time and end time of each software run.
3. The graphic font class iconfont.h is used to set the graphic font icon.

Common components of Qt :

  • QtitanRibbon : Ribbon UI components that follow the Microsoft Ribbon UI Paradigm for Qt technology, dedicated to providing full-featured Ribbon components for Windows, Linux and Mac OS X.
  • QtitanChart : is a C++ library that represents a set of controls that allow you to quickly provide beautiful and rich charts to your application. And supports all major desktop operating systems.
  • QtitanDataGrid : This Qt data grid component is created using pure C++, runs extremely fast, and has outstanding effects in processing big data and very large data sets. QtitanDataGrid is fully integrated with QtDesigner, so it is easy to adapt to other similar development environments, ensuring 100% compatibility with Qt GUI.

#### To be continued and continue to be updated

====================================================

Qt technical exchange group is now open, QQ search group number "765444821 " or scan the QR code below to join

Reprinted from: liudianwu

Guess you like

Origin blog.csdn.net/qq_42444778/article/details/115162165