The vc of Qt6 calls the dll with ui generated by qt

Benefiting from the cross-platform feature, the various language calls of dll generated by Qt are vividly reflected. The previous article has demonstrated the dll without ui generated by qt and how vc calls it. This article will demonstrate how vc calls the ui generated by qt dll.

The traditional view is that the dll with ui generated by Qt is limited by the exec method of QApplication, so that a message loop can be generated, so the DllMain function can only be rewritten in QTWinmigrate, but the solution is too cumbersome and redundant, so it is subject to the previous article Inspired, the following attempts were made, and it worked.

1. Create a new qt widgets project

Because it is inconvenient for us to debug and layout the interface in the qt dll project, the recommended solution is to create a new qt widgets project.

Layout the ui in this project, create a new mainwindow project as shown in the figure below, without adding any line of code, just drag out two controls in the control editor, and then connect them with signal slots, so that when you slide the slider below, the above The progress bar can change accordingly.

1. The ui designer is as follows;

 2. The source code is as follows, as you can see, it is all automatically generated after the qt craeator wizard builds the project, without manually rewriting any line of code;

 3. Switch to Release compilation and run normally;

 2. Create a new dll project

This step can be carried out completely according to the wizard;

1. Create a new library project;

2. Pay attention to check the qt widgets module;

 3. Try to include QMainWindow.h, if it does not report an error, it will be fine;

 4. Copy all the three files .h .cpp .ui in the widgets project created in the first step (note that main.cpp is not required) to the project directory and add them to the project, as shown in the figure below;

 5. In the dll project, declare a member function, which must be a function with an int parameter and a return value, and then define it as follows;

 6. The header file originally included in main.cpp is also included here, and then the main function is modified as follows:

//The following is the original main.cpp, which can be compared with the following. In fact, the main function is replaced with a member function, and the formal parameters are slightly changed to allow the qt event loop to continue to exist 

 

 7. Also switch to Release, and then build. Finally, on the right side of the figure, dll and .lib are generated, and all temporary files except these two files are deleted;

 8. Find the command line corresponding to the compiler, and then switch to the directory where the dll is located in the command;

 9. The qt command line solves the qt dll that the dll with ui still depends on. After success, it is as shown in Figure 2;

 

 3. VC project call

1. Create a new console project, switch to Release x64, generate an exe, and confirm that the project is correct;

2. Copy all the files generated in step 9 above to the vc project exe output folder;

 3. Copy the .lib file generated in step 9 and the two .h brought by the dll project (not needed for copying later), all to the VS project directory, and add .lib to resources, and add .h to the project ;

4. Directly shield the Qt header files used by the two .h, and add a red box to the file at the end of global.h as follows:

#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#  define Q_DECL_EXPORT __declspec(dllexport)
#  define Q_DECL_IMPORT __declspec(dllimport)
#else
#  define Q_DECL_EXPORT     __attribute__((visibility("default")))
#  define Q_DECL_IMPORT     __attribute__((visibility("default")))
#endif

 5. Include the header file, instantiate the class and call it, as shown in Figure 2 below, you can see the perfect dll generated by calling qt with ui through VC++;

 6. Copy the Release to a computer without a compilation environment, and test it, and it can also run normally;

 To sum up, it is completed so far. The vc project calls the dll with ui generated by qt, which is easier to understand and call than the method of rewriting the DllMain function in QTWinmigrate.

This project does not use threads in order to demonstrate, because of the event loop mechanism of qt, in fact, it is more reasonable to create a new thread and call it inside the thread.

Guess you like

Origin blog.csdn.net/yanchenyu365/article/details/131090043