The road to Qt development–modular design.pri file

The road to Qt development–modular design.pri file

  • The difference between QT pro files and pri files
  • Chapter1 Qt development road–modular design.pri file
    • 1: Introduction to .pri file
    • 2: Modular design through .pri
    • Three: Ending
  • Chapter2 When developing large-scale projects with Qt, many files are classified and displayed according to functional modules through .pri files.
  • Chapter3 Multi-pro and multi-pri cooperative programming in Qt ($$$)
  • Chapter4 Detailed explanation of Qt project file Pri ($$$)
    • 1. Introduction to Pri files
    • 2. Basics of writing Pri files
      • 1. Definition of variables
      • 2. Use of functions
    • 3. Advanced application of Pri files
      • 1. How to specify the compiler
      • 2. How to generate dynamic libraries and static libraries
      • 3. How to generate executable files and file copies
    • 4. Use cases of Pri files



The difference between QT pro files and pri files

The pro file is a project file, which is the supreme leader of the project that can be executed independently;

The pri file is much simpler than the pro file. It is purely for the editor’s logical distinction or clarification of ideas, and does not have the conditions for independent execution

Generally pri files contain:

HEADERS + = \
   page/systemsetup.h \
   
 
SOURCES + = \
   page/systemsetup.cpp \

That is, its header files and source files;

In fact, where pri files are used, pro files can be used instead, but the former is simple and practical;

Chapter1 Qt Development Road – Modular Design.pri File

Original link: https://blog.csdn.net/weixin_43229139/article/details/110874907

1: Introduction to .pri files

i is? The first letter of include. Similar to the header files in C and C++, we can put part of the content in the *.pro file into a *.pri file and then include it.

For example, we separate the settings of the source file and place them in the propriprfprl.pri file:

SOURCES + = main.cpp/
        widget.cpp
HEADERS + = widget.h
FORMS + = widget.ui

At this time, our propriprfprl.pro file can be simplified to:

TEMPLATE = app
CONFIG + = QT
QT + = core gui

TARGET = propriprfprl
include(propriprfprl.pri)

If a larger project contains multiple *.pro files, these pro files need to have some common settings or files, which is necessary at this time.

Two: Modular design through .pri

For QT projects, all files are developed in the same folder by default. When a project is relatively large in scale, there are many files, which makes maintenance difficult. Therefore, pri is very important for modular development of files. , the modularization of pri is nothing more than putting the code of the same business logic into the same folder for management.
When the number of your project files increases, the logical structure of your project will become relatively complex. When you click on your project node, you will find that there is such a long string under the node, and the files of all modules are squeezed under one node. , very bloated. As shown below, the comparison before and after modularization:

Step 1: Go to the project root directory and create a new folder of your own business logic. The Dialog and Widget folders are as shown below:

Step 2: Move the same function files to the corresponding folder.


Step 3: Create a new “.pri” file under the newly created folder, and create a new .pri file name rule (folder name + .pri). After creating the .pri file, add the content manually, or add it according to step 5 .

Step 4: Add the following content to the .pro file in the project root directory.
INCLUDEPATH
include

QT + = core gui

greaterThan(QT_MAJOR_VERSION, 4): QT + = widgets

TARGET = TestQt
TEMPLATE = app

SOURCES + = main.cpp\
        mainwindow.cpp
HEADERS + = mainwindow.h
FORMS += mainwindow.ui

RESOURCES + = \
    image.qrc
DISTFILES + =

INCLUDEPATH + = $$PWD/Dialog # When the project is compiled, the directory under the INCLUDEPATH list will be searched for files.
include ($$PWD/Dialog/Dialog.pri) # The files included in include() will be displayed in the project structure diagram

INCLUDEPATH + = $$PWD/Widget # When the project is compiled, the directory under the INCLUDEPATH list will be searched for files.
include ($$PWD/Widget/Widget.pri) # The files included in include() will be displayed in the project structure diagram

Step 5: After qmake, right-click Dialog and Widget to add files in the corresponding folders. The contents of the .pri file are as follows:
Dialog.pri file:

FORMS + = \
    $$PWD/dialogone.ui \
    $$PWD/dialogtwo.ui

HEADERS + = \
    $$PWD/dialogone.h \
    $$PWD/dialogtwo.h

SOURCES + = \
    $$PWD/dialogone.cpp \
    $$PWD/dialogtwo.cpp

Widget.pri file:

FORMS + = \
    $$PWD/widgetone.ui \
    $$PWD/widgettwo.ui

HEADERS + = \
    $$PWD/widgetone.h \
    $$PWD/widgettwo.h

SOURCES + = \
    $$PWD/widgetone.cpp \
    $$PWD/widgettwo.cpp

Step Six: Renderings

Three: Ending

Writing a .pro file is very helpful for you to sort out the structure of the project. A good framework often has a multiplier effect on development and maintenance.

When Chapter2 Qt develops large-scale projects, many files are classified and displayed by functional modules through .pri files

Original link


When we need to maintain the code later, if we want to find the code for a certain function, we can first find the directory of the corresponding function module, and then find the corresponding class file in that directory. This is a proven technique for improving development productivity!

Chapter3 Multi-pro and multi-pri cooperative programming in Qt ($$$)

Original link: https://blog.csdn.net/u014597198/article/details/52679142

There is no essential difference between the ri file and the pro file. They both play the role of including paths. The difference is that the pro file is the main file and pri is the subsidiary file.

If the project is relatively complex, such as multi-platform development, you need to use multiple pri and pro to work together. Of course, it can also be edited into a static library or a dynamic library. Now let’s talk about how to implement multiple pri.

A pro can contain multiple pri, for example:
include(win32.pri)
include(macx.pri)
include(ios.pri)
include(android.pri)

Looking at the usage of pro and pri in two actual projects, I hope it can provide some reference value for your architecture design.

Example 1: (Design architecture according to function)

Example 2: (Architected according to platform and main functions)

Chapter4 Qt project file Pri detailed explanation ($$$)

Original link: https://blog.csdn.net/thanklife/article/details/131021462

In the Qt project, the pri file (.pri) is a file similar to a makefile, which is used to define the compilation rules in the Qt project. Usually you can use pri files to configure Qt library, header files, source files, link libraries and other information. This way you can define this information in one file to avoid repeated configuration in each project and improve the reusability of the code. .

For Qt projects, the pri file is very important because it can facilitate module management and make the structure of the project clearer. If you need to add new modules or new third-party libraries to the Qt project, you can add relevant information in the pri file. At the same time, the pri file can also make code compilation more efficient because it automatically selects the most optimized compiler and compilation options according to different platforms.

When using pri files, you should learn how to write and use your own pri files so that you can customize the configuration according to your needs. At the same time, you can also share the pri files you have written with others to improve the development efficiency of the entire team. In the Qt Creator editor, you can right-click the project folder, select the “Add New…” menu, select “Add Existing Files or Directory…”, and then select the corresponding pri file to add to the project.

It should be noted that although pri files are very powerful, they also need to be used with caution. If you configure the pri file incorrectly, the code will not compile and run normally, so you must be careful when modifying the pri file.

1. Introduction to Pri files

In Qt, a Pri file is a project file used to link C++ code, JavaScript files, resource files, etc. together and generate an executable file. Pri files can define compilation rules, linking rules, generation rules, deployment rules, etc., and implement various complex build processes and deployment processes by using different variables and functions.

Pri files are based on Makefile syntax, so certain Makefile knowledge is required. At the same time, the Qt Creator integrated development environment and its command line tool Qt Build System (qbs) can easily generate Pri files and build them, making it possible to develop and build quickly without writing a complete Makefile.

2. Basics of writing Pri files

The writing of Pri files is inseparable from basic syntax such as variables, functions, and conditional judgments.

1. Definition of variables

The variables in the Pri file can be predefined system variables, such as $$PWD indicating the directory where the Pri file is located; or they can be user-defined variables, such as SRC_DIR indicating the source file directory. The syntax for variable definition is:

VARIABLE_NAME = variable_value

Among them, VARIABLE_NAME is the variable name and variable_value is the value of the variable. Within the value of a variable, other variables can be embedded, for example:

SRC_DIR = $$PWD/src
OBJ_DIR = $$PWD/obj
OBJECTS_DIR = $$OBJ_DIR/$$TARGET

Among them, $$TARGET represents the target type of the generated file, which can be exe, dll, plugin, etc.

2. Use of functions

The functions in Pri files can greatly simplify complex build processes, such as file search, file generation, file copy, library linking, etc.

The syntax of the function is:

FUNCTION_NAME(arg1, arg2, ...)

Among them, FUNCTION_NAME is the function name, arg1, arg2, etc. are the parameters of the function. Commonly used functions include:

files: used to search all files in the specified directory
subdirs: Specify subdirectories under the current directory
target.sources: Specify source files for target files
target.depends: Specifies the files that the target file depends on
target.commands: Specify compilation commands, link commands, etc. for the target file
target.path: Specifies the path to generate the target file
target.link: Specify the library files and link commands to be linked to the target file, etc.
For example:

LIBS + = -lfoo
target_link($$TARGET, $$LIBS)

Among them, target_link is a custom function used to
The libraries specified in LIBS are linked to TARGET.

  1. Conditional judgment
    In the Pri file, conditional judgments can be made on different operations based on specific circumstances, for example:
win32:LIBS + = -lwsock32
else:LIBS + = -lsocket -lnsl

Among them, win32 means operating under Windows platform, else means operating under other platforms.

3. Advanced application of Pri files

After mastering the basic Pri file syntax, Pri files can perform more complex compilation tasks.

1. How to specify the compiler

In the Pri file, the compiler can be specified by setting the QMAKE_CXX and QMAKE_CC variables:

QMAKE_CXX = g++
QMAKE_CC = gcc

2. How to generate dynamic libraries and static libraries

In the Pri file, you can specify the generation rules of dynamic libraries and static libraries through target.commands:

lib.path = /usr/lib
lib.name = foo
lib.files = source1.cpp source2.cpp
lib.commands = $$QMAKE_CXX -shared -o $$lib.target $$OBJECTS

Among them, lib.path specifies the path to generate the library file, lib.name specifies the name of the library file, lib.files specifies the source file of the library file, and lib.commands specifies the generation command of the library file.

3. How to generate executable files and file copies

The rules for generating executable files and file copies are as follows:

executable.output = foo
executable.files = source1.cpp source2.cpp
executable.commands = $$QMAKE_CXX -o $$executable.target $$OBJECTS
INSTALLS + = exe
exe.target = /usr/bin
exe.files = $$executable.target
exe.commands = $$QMAKE_COPY $$executable.target $$exe.target

Among them, executable.output specifies the name of the executable file, executable.files specifies the source file of the executable file, and executable.commands specifies the generation command of the executable file. INSTALLS specifies the deployment rules for generating files, exe.target specifies the deployment target directory, exe.files specifies the files to be deployed, and exe.commands specifies the command for file copying.

4. Use cases of Pri files

Here is an example of a simple Pri file:

SRC_DIR = $$PWD/src
OBJ_DIR = $$PWD/obj
TARGET_DIR = $$PWD/bin
 
INCLUDEPATH + = $$PWD/include
LIBS + = -ldl
 
target.path = $$TARGET_DIR
target.name = myapp
 
message(Building ... $$TARGET)
message(Source files: $$SOURCES)
 
contains(CONFIG, debug) {<!-- -->
    message(Building in debug mode...)
 
    TARGET = myappd
 
    COMMON_FLAGS = -g -O0
}
 
contains(CONFIG, release) {<!-- -->
    message(Building in release mode...)
 
    TARGET = myapp
 
    COMMON_FLAGS = -O2
}
 
sources = $$files($$SRC_DIR/*.cpp)
 
target.sources = $$sources
 
OBJECTS_DIR = $$OBJ_DIR/$$TARGET
 
mocable_headers = $$_PRO_FILE_PWD_/input.h
mocables = $$mocable_headers
mocables.headers = $$mocable_headers
mocables.commands = $$QTDIR/bin/moc $$mocables.headers -o $$mocables.target
 
QMAKE_EXTRA_TARGETS + = mocables
 
unix {<!-- -->
    target.commands = $$QMAKE_CXX -o $$TARGET $$OBJECTS $$LIBS
    mocables.target = $${OBJECTS_DIR}/input.moc
    mkdir($${OBJECTS_DIR})
} else {<!-- -->
    target.commands = link -out:$$TARGET.exe $$OBJECTS $$LIBS
    mocables.target = $${OBJECTS_DIR}/input.moc.obj
    mkdir($$system_path($${OBJECTS_DIR}))
}
 
target.dependency_type = TYPE_C
 
target.CONFIG + = dll
target.LIBS + = -lfoo
 
DISTFILES + = $$PWD/post_install_script
 
mocables.CONFIG += no_link_target
 
INSTALLS + = target mocables
target.files = $$TARGET
mocables.files = $${mocable_headers}

Among them, the Pri file implements basic compilation and deployment tasks. The specific implementation method is as follows:

  • Three variables, SRC_DIR, OBJ_DIR, and TARGET_DIR, are defined to specify the source file directory, target file directory, and executable file directory.
  • The header file search path and library file search path are specified.
  • The BUILD_MODE variable is defined to specify the build mode, debug or release.
  • According to the setting of BUILD_MODE, the name of the executable file and the compilation options are determined.
  • Specify the source file for the target file based on all .cpp files in the source file directory.
  • Specifies the header files that need to be run by the moc precompiler to generate mocable files.
  • Specifies the link library, library file, etc. of the executable file.
  • Deployment rules for the executable are configured.

Through this simple Pri file, we can see the power and convenience of Qt Pri. In actual development, Pri files can be customized according to needs and support multi-library compilation, multi-platform compilation, resource file compilation, etc., which greatly improves development efficiency.