Qt project architecture experience summary

(1) General rules

  1. Except for very small demo-level projects, it is recommended to use pri to store code files in different folders, which is convenient for unified management and search.
  2. It is recommended to put the classes of the same type of functions together. If there are too many code files in the directory, it is also recommended to split multiple directories for storage.
  3. For example, for 3-5 interface projects, 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, log management The 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 members allocate different functions, you can consider the form of plug-ins. Generally, two types of plug-ins are used. One is the plug-in in the form of a common dynamic library, which must be put together with the main program; the other is the Qt mechanism The plug-in 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 easy it is, 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 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., and if there is a problem, the configuration file is regenerated to avoid malicious deletion of the configuration file causing abnormal program operation.
  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 the initial failure to read the node. The configuration item value does not match 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, such as the timer interval is 0, and you need to correct the setting again Is 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, the configuration file code should be set to utf-8, set.setIniCodec("utf-8").

(Three) 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 place 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 to c. , C is then sent 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. Send the signal of class a to the appevent class, and then class d is directly associated with the appevent class for processing.
  4. The larger the project, the more necessary it is 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 a global style sheet, it is recommended to read the general style sheet first, and then add the content of the additional style sheet to the back and set it together.
  4. Set the project code.
  5. Set translation files, you can load multiple, including qt_zh_CN.qm built-in qt, user's own translation files, 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, 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 recording class is used to record the start time and end time of each software run.
  11. The associated global event filter handles 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 network print output.
  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.

Continued to be updated

Guess you like

Origin blog.csdn.net/feiyangqingyun/article/details/113985170