Qt-based asynchronous remote invocation development system and method
By using a Qt-based asynchronous remote call development framework, module decoupling and asynchronous calls are achieved, solving the problem of strong module dependencies in the Qt development framework, simplifying logical complexity, supporting function calls in a multi-threaded environment, and improving code readability and scalability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUODIAN NANJING AUTOMATION
- Filing Date
- 2023-08-16
- Publication Date
- 2026-05-12
AI Technical Summary
In the Qt development framework, strong dependencies between modules lead to high design, communication and implementation costs, and the logic complexity increases during asynchronous interaction, making it difficult to call non-thread-safe functions in a multi-threaded environment.
It adopts an asynchronous remote call development framework based on Qt, achieves module decoupling through a message sending and receiving development framework, uses reflection to find and call functions, and asynchronously calls the functions of the called module through the QEventLoop object, supporting multi-threaded parallel calls.
It reduces the communication and collaboration costs between modules, improves code readability and scalability, simplifies logic decomposition, and adapts to non-thread-safe function calls in multi-threaded environments.
Smart Images

Figure CN117075856B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of software systems, in particular to a development framework and method for asynchronous remote invocation based on Qt. BACKGROUND
[0002] Modern large software systems are often very complex, composed of various software sub-modules (modules) divided according to business flow, data flow, etc. standards, and there is a certain coupling relationship between the business logic and data dependency of different modules. At the same time, as the complexity of the software system increases, different modules are often responsible for different development, testing and operation and maintenance teams.
[0003] Therefore, the increase in coupling will bring great challenges to software maintenance and testing. This is more prominent in large C++ software project engineering using the QT development framework. For example, if module A needs to call the interface of module B under the QT development framework, it will cause module A to have a code-level strong dependency on module B, including the following three aspects: module A needs to directly reference the header file of module B, in the header file of module B, the interface called by module A needs to be explicitly declared, and when module A is compiled, the interface of module B called needs to have the corresponding implementation.
[0004] The above strong dependency will bring more design, communication and implementation costs in the actual development process. Therefore, to solve the problem of strong dependency between modules when calling each other, a message receiving and sending development framework is developed, so that when modules need to call each other, different modules are no longer directly dependent at the code level, and a unified message forwarding management engine module is responsible for interaction, thereby realizing the decoupling of the software system at the code level, improving the readability, reusability and extensibility of the code, and reducing the communication and collaboration costs between different module developers. However, if the interaction needs to return a value, the calling and returning need to be completed in two parts of logic, which increases the implementation complexity. SUMMARY
[0005] This section is intended to summarize some aspects of the embodiments of the present application and briefly introduce some preferred embodiments. Some simplifications or omissions may be made in this section and the abstract and title of the specification of the present application in order to avoid obscuring the purpose of this section, the abstract and the title, and such simplifications or omissions cannot be used to limit the scope of the present application.
[0006] Therefore, the purpose of this invention is to provide a development framework and method for asynchronous remote calls based on Qt. Based on the "Qt-based message sending and receiving development framework", it solves the above-mentioned logic decomposition problem, reduces implementation complexity, and allows non-thread-safe "called functions" to be called in a multi-threaded environment, adapting to some special application scenarios.
[0007] To address the aforementioned technical problems, according to one aspect of the present invention, the present invention provides the following technical solution:
[0008] An asynchronous remote call development framework based on Qt, comprising: a base class and a public call function `callFunc`.
[0009] The base class implements the callSelfFunc function, which uses Qt's reflection mechanism to find and call the function, and the called module inherits from this base class;
[0010] The public call function "callFunc" finds the called module object based on the module name, generates a QEventLoop object, and asynchronously calls the callSelfFunc function of the called module through the invokeMethod provided by Qt.
[0011] As a preferred embodiment of the method of the Qt-based asynchronous remote call development framework described in this invention, the public call function callFunc is a parallel call function provided by the development framework to other modules, supporting parallel calls in multiple threads.
[0012] In a preferred embodiment of the method for developing an asynchronous remote call framework based on Qt as described in this invention, the QEventLoop object is a thread event loop object provided by Qt.
[0013] As a preferred embodiment of the method for developing an asynchronous remote call framework based on Qt as described in this invention, it further includes a message sending and receiving development framework, a calling module, and a called module.
[0014] During software initialization, the message sending and receiving development framework generates called modules. The called modules inherit the library file base class QObject and store the pointers of the called objects of the calling modules into the called module collection according to <module name, pointer of called object>.
[0015] The called module inherits from QObject and adds the Q_OBJECT macro, and the functions of the called module are added with Q_INVOKABLE or declared under public slots.
[0016] As a preferred embodiment of the method for developing an asynchronous remote call framework based on Qt according to the present invention, the message sending and receiving development framework includes:
[0017] A message sending and receiving base class is provided, which includes a message sending function sendData() and a message receiving function recvData() that provide message sending and receiving functionality.
[0018] The configuration file provides two types of configuration information. The first type of configuration information includes the library name of the module and the name of the generated module. The second type of configuration information includes the message dependencies between modules, which are represented as: <sending module name, message command, receiving module name>.
[0019] The message forwarding management engine reads configuration information from the configuration file, generates a module object based on the library name of a module and the name of the module to be generated, and establishes a message topology between modules based on the message dependencies between modules.
[0020] As a preferred embodiment of the development framework method for asynchronous remote calls based on Qt described in this invention, the QEventLoop object inherits from the QObject class, temporarily stores data in the object through the setProperty function, and retrieves the temporarily stored data through the property function.
[0021] A method for developing an asynchronous remote call framework based on Qt, with the following steps:
[0022] S1. The module calls the parallel calling function callFunc provided by the development framework in thread 1, passing in the module name and the calling function name;
[0023] S2. In the callFunc function, the called module object is found based on the passed module name. If found, proceed to the next step; otherwise, return directly.
[0024] S3. Check if the called module defines the callSelfFunc function. If found, proceed to the next step; otherwise, return directly.
[0025] S4. Generate a QEventLoop object, and asynchronously call the callSelfFunc method of the called module through the invokeMethod method provided by Qt, passing in the name of the called function and the QEventLoop object, and calling the exec function of the QEventLoop object to enter the event loop, causing callFunc to block.
[0026] S5. The "callSelfFunc" method of the called module is triggered in thread 2. It searches for the function entity based on the function name. If found, proceed to step S5; otherwise, proceed to step S6.
[0027] S6. Call the function of the called module in thread 2 and obtain the return value;
[0028] S7. Fill the return value into the QEventLoop object, and asynchronously call the quit function of the QEventLoop object through the invokeMethod method provided by Qt;
[0029] S8. The exec function of the QEventLoop object called in step S4 returns due to the quit function called in step S7. Then, the return value is obtained from the QEventLoop object, and the callFunc function returns to the calling module with the return value.
[0030] Compared with the prior art, the beneficial effects of this invention are: the Qt-based asynchronous remote call development framework is based on the Qt message sending and receiving development framework. It calls the return value of the called module through the message sending and receiving development framework and returns it synchronously to the calling module, which solves the above-mentioned logic splitting problem, reduces the implementation complexity, and can call non-thread-safe "called functions" in a multi-threaded environment, adapting to some special application scenarios. Attached Figure Description
[0031] To more clearly illustrate the technical solutions of the embodiments of the present invention, the present invention will be described in detail below with reference to the accompanying drawings and detailed embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. Wherein:
[0032] Figure 1 This is a schematic diagram of an asynchronous remote call development framework based on Qt according to the present invention;
[0033] Figure 2 This is a schematic diagram illustrating the interaction between the development framework and library files for an asynchronous remote call based on Qt according to the present invention.
[0034] Figure 3 This is a schematic diagram illustrating a scenario for a development framework for asynchronous remote calls based on Qt, as described in this invention.
[0035] Figure 4 This is a block diagram of a method for developing an asynchronous remote call framework based on Qt according to the present invention. Detailed Implementation
[0036] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
[0037] Secondly, the present invention is described in detail with reference to the schematic diagrams. When detailing the embodiments of the present invention, for ease of explanation, the cross-sectional views illustrating the device structure may be partially enlarged, not according to the usual scale. Furthermore, the schematic diagrams are merely examples and should not limit the scope of protection of the present invention. In addition, actual fabrication should include three-dimensional spatial dimensions of length, width, and depth.
[0038] To make the objectives, technical solutions, and advantages of the present invention clearer, the embodiments of the present invention will be described in further detail below with reference to the accompanying drawings.
[0039] In the QT development framework, if module A needs to call the interface of module B, it will cause module A to have a strong code-level dependency on module B. The strong dependency includes the following three aspects: module A needs to directly reference the header file of module B; the header file of module B needs to explicitly declare the interface called by module A; and when module A is compiled, it needs to have a corresponding implementation of the interface of module B that is called.
[0040] For example: Engineer A develops module a, and Engineer B develops module b. Module a depends on module b. Engineer B needs to define the header file of module b first, and have an empty implementation in the source file. After compiling it into a library file and providing it to Engineer A, Engineer A can import the header file of module b into module a before compiling module a.
[0041] For example, engineer A develops module a, and engineer B develops module b. Module a depends on module b, and module b depends on module a. Because modules a and b are interdependent, engineer A and engineer B can only implement the functions of modules a and b in the same project, and cannot develop them in separate projects.
[0042] The strong dependencies mentioned above will lead to higher design, communication, and implementation costs in the actual development process.
[0043] To address this, a Qt-based message sending and receiving development framework is provided. Different modules no longer depend on each other at the code level, but interact through a unified message forwarding management engine module. This achieves code-level decoupling of the software system, improves code readability, reusability, and scalability, and reduces communication and collaboration costs between developers of different modules.
[0044] The message sending and receiving development framework includes a message sending and receiving base class, configuration files, and a message forwarding management engine. The message sending and receiving base class provides a base class that includes a message sending function sendData() and a message receiving function recvData() that provide message sending and receiving functionality.
[0045] The configuration file is provided in XML format and provides two types of configuration information. The first type of configuration information includes the library name of the module and the name of the generated module. The second type of configuration information includes the message dependencies between modules, which are represented as: <sending module name, message command, receiving module name>.
[0046] The message forwarding management engine, based on registration information, is responsible for managing message forwarding between modules. It reads configuration information from the configuration file, generates module objects based on the library name of a module and the name of the module to be generated, and establishes the message topology between modules based on the message dependencies between modules.
[0047] However, in the process of using the above message sending and receiving development framework, if the interaction requires a return value, the logic before the call and the return need to be completed in two parts, which increases the implementation complexity. Based on this, the present invention provides a development framework and method for asynchronous remote calls based on Qt. On the basis of the "Qt-based message sending and receiving development framework", the above logic splitting problem is solved, the implementation complexity is reduced, and non-thread-safe "called functions" can be called in a multi-threaded environment, adapting to some special application scenarios.
[0048] This invention is based on Qt's reflection mechanism. In Qt's reflection mechanism, functions can be found and called using function names represented by strings. The called module can find and call the function through Qt's reflection mechanism by following the steps below:
[0049] 1) The called module inherits from QObject and adds the Q_OBJECT macro;
[0050] 2) If the function of the called module is declared with Q_INVOKABLE or under public slots, the calling module can then use the invokeMethod() function provided by Qt to call it via a string.
[0051] like Figure 1As shown, this Qt-based asynchronous remote call development framework includes a base class and a public call function `callFunc`. The base class implements the `callSelfFunc` function, which uses Qt's reflection mechanism to find and call functions, and the called module inherits from this base class. The public call function `callFunc` finds the called module object based on the module name, generates a `QEventLoop` object, and asynchronously calls the called module's `callSelfFunc` function using Qt's `invokeMethod`. The public call function `callFunc` is a parallel call function provided by the development framework for other modules, supporting parallel calls in multiple threads. The `QEventLoop` object is a thread event loop object provided by Qt, which inherits from the `QObject` class. The `setProperty` function is used to temporarily store data in the object, and the `property` function is used to retrieve the temporarily stored data.
[0052] This Qt-based asynchronous remote call development framework also includes a message sending and receiving framework, a calling module, and a called module; such as Figure 2 As shown, during software initialization, the message sending and receiving development framework generates called modules. The called modules inherit from the library file base class QObject. The pointers of the called objects of the calling modules are stored in the called module collection according to <module name, pointer of called object>. The called modules inherit from QObject and add the Q_OBJECT macro. The functions of the called modules are added with Q_INVOKABLE or declared under public slots.
[0053] To elaborate on the Qt-based synchronous remote call development framework, this invention also provides a method for a Qt-based asynchronous remote call development framework. Please refer to [link to relevant documentation]. Figures 3-4 A method for developing an asynchronous remote call framework based on Qt, with the following specific steps:
[0054] S1. The calling module calls the parallel calling function callFunc provided by the development framework in thread 1 (thread 1 is the calling thread), passing in the module name and the calling function name;
[0055] S2. In the callFunc function, the called module object is found based on the passed module name. If found, proceed to the next step; otherwise, return directly.
[0056] S3. Check if the called module defines the callSelfFunc function. If found, proceed to the next step; otherwise, return directly.
[0057] S4. Generate a QEventLoop object, and asynchronously call the callSelfFunc method of the called module through the invokeMethod method provided by Qt, passing in the name of the called function and the QEventLoop object, and calling the exec function of the QEventLoop object to enter the event loop, causing callFunc to block.
[0058] S5. The "callSelfFunc" method of the called module is triggered in thread 2 (thread 2 is the execution thread). It searches for the function entity based on the called function name. If found, proceed to step S5; otherwise, proceed to step S6.
[0059] S6. Call the function of the called module in thread 2 and obtain the return value;
[0060] S7. Fill the return value into the QEventLoop object, and asynchronously call the quit function of the QEventLoop object through the invokeMethod method provided by Qt;
[0061] S8. The exec function of the QEventLoop object called in step S4 returns due to the quit function called in step S7. Then, the return value is obtained from the QEventLoop object, and the callFunc function returns to the calling module with the return value.
[0062] Although the present invention has been described above with reference to embodiments, various modifications can be made and components can be replaced with equivalents without departing from the scope of the invention. In particular, as long as there is no structural conflict, the features in the disclosed embodiments can be combined with each other in any manner. The lack of an exhaustive description of these combinations in this specification is merely for the sake of brevity and resource conservation. Therefore, the present invention is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.
Claims
1. A development system for asynchronous remote calls based on Qt, characterized in that, include: Base class and public call function callFunc, The base class implements the callSelfFunc function, which uses Qt's reflection mechanism to find and call the function, and the called module inherits from this base class; The public call function callFunc finds the called module object based on the module name, generates a QEventLoop object, and asynchronously calls the callSelfFunc function of the called module through the invokeMethod provided by Qt. The public call function callFunc is a parallel call function provided by the development framework to other modules, which supports parallel calls in multiple threads; It also includes a message sending and receiving development framework, a calling module, and a called module; During software initialization, the message sending and receiving development framework generates called modules. The called modules inherit the library file base class QObject and store the pointers of the called objects of the calling modules into the called module collection according to <module name, pointer of called object>. The called module inherits from QObject and adds the Q_OBJECT macro, and the functions of the called module are added with Q_INVOKABLE or declared under public slots; The message sending and receiving development framework includes: A message sending and receiving base class is provided, which includes a message sending function sendData() and a message receiving function recvData() that provide message sending and receiving functionality. The configuration file provides two types of configuration information. The first type of configuration information includes the library name of the module and the name of the generated module. The second type of configuration information includes the message dependencies between modules, which are represented as: <sending module name, message command, receiving module name>. The message forwarding management engine reads configuration information from the configuration file, generates a module object based on the library name of a module and the name of the module to be generated, and establishes a message topology between modules based on the message dependencies between modules.
2. The Qt-based asynchronous remote call development system according to claim 1, characterized in that, The QEventLoop object is a thread event loop object provided by Qt.
3. The Qt-based asynchronous remote call development system according to claim 1, characterized in that, The QEventLoop object inherits from the QObject class. It uses the setProperty function to temporarily store data in the object and uses the property function to retrieve the temporarily stored data.
4. A method for developing an asynchronous remote call system based on Qt as described in any one of claims 1-3, characterized in that, The steps are as follows: S1. The module calls the parallel calling function callFunc provided by the development framework in thread 1, passing in the module name and the calling function name; S2. In the callFunc function, the called module object is found based on the passed module name. If found, proceed to the next step; otherwise, return directly. S3. Check if the called module defines the callSelfFunc function. If found, proceed to the next step; otherwise, return directly. S4. Generate a QEventLoop object, and asynchronously call the callSelfFunc method of the called module through the invokeMethod method provided by Qt, passing in the name of the called function and the QEventLoop object, and calling the exec function of the QEventLoop object to enter the event loop, causing callFunc to block. S5. The callSelfFunc method of the called module is triggered in thread 2. It searches for the function entity based on the function name. If found, proceed to step S5; otherwise, proceed to step S6. S6. Call the function of the called module in thread 2 and obtain the return value; S7. Fill the return value into the QEventLoop object, and asynchronously call the quit function of the QEventLoop object through the invokeMethod method provided by Qt; S8. The exec function of the QEventLoop object called in step S4 returns due to the quit function called in step S7. Then, the return value is obtained from the QEventLoop object, and the callFunc function returns to the calling module with the return value.