Common development steps for QT project code to remove UI interface

Common development steps for QT project code to remove UI interface
     Due to project development needs, the leader requires that the entire QT project should not use UI to implement the interface. This can ensure the stability of program operation and For the logic and readability of the code, first record the specific steps as follows:

     1. First, we realize the design effect of the interface by dragging controls, then compile the project and run it to ensure that the operation interface can be opened normally when the project is running. For example, the common login interface effect is as follows:

   2. Open the header file corresponding to the UI interface. After the project is successfully compiled, the .h code file corresponding to the interface UI is automatically generated. For example, the login interface is designed through the UI. Normally there are three code files, namely loginform.h and loginform. .cpp, loginform.ui. After the project is compiled and run, the system will automatically generate a ui_loginform.h based on the loginform.ui file. We found the loginform.h code file in the project. After opening it, we found the following code:

Hold down the Ctrl key and click the LoginForm in the namespace Ui {class LoginForm;} code with the mouse. The ui_loginform.h file in the Debug directory will open, as shown below:

   3. Copy the corresponding code in the header file generated by the UI interface to our project code file, which mainly includes three parts of code as follows.
   The first part is the variable definition corresponding to the code:

   
 

The second part is the code corresponding to the detailed settings of the controls corresponding to the interface setupUi(QWidget * xxx) function:

   The third part is the code corresponding to the translation retranslateUi(QWidget * xxx) function :

 

 The code for the first part of the control variable definition is copied to the private: code in the loginform.h file of our login interface header file, as shown below:

Add initialization and translation function definitions under public:: void setupUi(QWidget *LoginForm);void setupUi(QWidget *LoginForm);

Then add the second part and the second part Copy the three parts of code to the loginform.cpp file, as shown in the figure:

 4. Delete the corresponding ui-related code in the header file and code file:

  • Delete the following relevant code in the loginform.h file:

namespace Ui {
class LoginForm;
}

Ui::LoginForm *ui;

  • Delete the following relevant code in the loginform.cpp file:

   a. Replace ui-> with the empty string "";
   b. Delete ui(new Ui::LoginForm) and code in the constructor
   c. Delete the delete ui of the destructor;

 5. After sequentially executing operations such as cleaning all projects, executing qmake, and rebuilding all projects in the QTCreator software build menu, run the project and find that the same effect as the original interface project has been successfully achieved. The entire UI file removal operation is now completed. , achieving the goal of having only .h and .cpp files in the project and no .ui files.​ 

 

 

Guess you like

Origin blog.csdn.net/xqf222/article/details/131951842