Qt project architecture: global class description

Here are some global classes, which are generally placed in the Util folder. Util means tool. Generally speaking, it is often used to describe data processing that has nothing to do with business logic.

1. Global configuration file

The global configuration file management class AppConfig is used to read and write configuration files of corresponding projects. The format can be ini, xml, json, etc. For small projects, ini is recommended, as convenient as it is, which is equivalent to mapping the value of the configuration file to a global variable.

If there are many configuration items in the configuration file, it is recommended to store them in groups for easy search, instead of putting them all in one big group. When reading the configuration file, you can judge whether the configuration file exists, whether the configuration item is missing, etc. If there is a problem, the configuration file will be regenerated to avoid malicious deletion of the configuration file and cause the program to run abnormally.

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 failure to read the node at the beginning. Configuration item value does not conform to expected value type.

After reading the configuration file, you can re-judge whether the value of the configuration item meets the requirements, filter and correct the value, and prevent the configuration file from being manually opened and filled with abnormal values. For example, the interval of the timer is 0, and the setting needs to be corrected again. is a legal value.

The initial value with Chinese is wrapped with QString::fromUtf8, such as QString::fromUtf8("administrator"). For configuration items with Chinese characters, set the configuration file encoding to utf-8, such as set.setIniCodec("utf-8").

2. Global variables

The global variable management class AppData is used to set all the global variables used in the project. For example, whether the current user/system is locked, etc., so that this variable can be used in any coding position for judgment processing. Variables such as the width and height of the navigation bar, button size, and icon size in the UI interface can be placed here, and different values ​​can be set by judging the resolution after the system starts.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

3. Global event transfer processing

The global event transfer processing class AppEvent is used to transfer various events across multiple UIs and multiple classes in the system. This class must be a global singleton class, which is convenient for global and unified use.

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. In the case of 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 nesting level is more and more complicated, the code will be more difficult to manage.

Send the signal of class a to the appEvent class, and then class d directly associates with the appEvent class for processing. The larger the project, the more you will find the necessity of signal transfer processing, the code is clear, and the management is convenient.

4. Global program initialization

The global program initialization class AppInit is used to do some initialization processing after the program starts.

  • To set a global style sheet, it is recommended to read the general style sheet first, and then add the additional style sheet content to the back and set it together.
  • Set translation files, you can load multiple, including qt_zh_CN.qm built in qt, user's own translation files, etc.
  • The startup log output class is used to start the log service.
  • Read the configuration file.
  • Set the global font.
  • Set the project encoding.
  • Initialize the random number seed.
  • Create the directory required in the new project to prevent files from being saved to the directory without the directory.
  • Initialize the database, including opening the database and loading basic data such as user tables and device tables.
  • The startup running time recording class is used to record the start time and end time of each software run.
  • Associate global event filters to handle custom borderless UI dragging, global button processing, etc.

Five, global general class

  • The global general class AppCommon defines some commonly used functions such as delay.
  • The debug log output class SaveLog is used to start the log service, which can output the log to a file or network printout.
  • The running time record class SaveRuntime is used to record the start time and end time of each software run.
  • The graphic font class IconFont is used to set the graphic font icon.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/QtCompany/article/details/131584197