Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation step of using QT to develop interface in MFC program

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people don't know what to do about the implementation steps of using QT development interface in MFC programs. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

If you have an off-the-shelf MFC project to do maintenance, but you are tired of using MFC tedious operations to do interface beautification, or you need to use some features that are easy to use in QT in this project; or you need to use some components that can only be used in MFC, but this part of the interface has been done with QT. Then this article may be able to help you

The demo environment uses Visual Studio 2019 + QT5.12.8 version

Add QT dependency

First of all, create a dialog box-based MFC project. Of course, other projects such as multi-document and single-document projects are also possible, but I am using dialog boxes here for simplicity.

Then click the project with the right mouse button, and then click Properties-- > C _ blank +-- > normally add the header file paths of QCore, QGui, QWidget and QT to the additional header files of the project.

Remember to select either 64-bit or 32-bit inclusion according to the corresponding compilation options.

Then add qt's lib library to the directory of additional libraries in Connector-> General.

Finally, add the dependent lib file in the connector-> input. It should be noted that the debug version needs to link the lib file with d, while release does not have d on the link.

Compile first, if there is no problem, the configuration related to qt has been completed.

Add signal slot mechanism

MFC is based on Windows message queue to handle and respond to ui events, while qt uses signal slot mechanism to respond. Although we have added the dependency of qt, we can only use other qt libraries and cannot use signal slots in qt. We need to add some additional components to make mfc support signal slots.

Fortunately, this part of the demand qt-related research and development personnel have taken into account, you can find QMfcApp in github

We can copy these two files and add them to the project. And add # include "pch.h" to the appropriate location of the cpp file to include the preprocessing header

There will be errors in the middle, because the string type in CString under the Unicode character set is wchar_t* QString::fromLocal8bit that cannot be converted from wchar_t* to char*, so you can modify it here, use QString::fromStdWString (), and then compile

You can see how to use it in the comments on QMfcApp.cpp

/ *! Creates an instance of QApplication, passing the command line of\ a mfcApp to the QApplication constructor, and returns the new object. The returned object must be destroyed by the caller. Use this static function if you want to perform additional initializations after creating the application object, or if you want to create Qt GUI elements in the InitInstance () reimplementation of CWinApp:\ code BOOL MyMfcApp::InitInstance () {/ / standard MFC initialization /... / / This sets the global qApp pointer QMfcApp::instance (this); / / Qt GUI initialization} BOOL MyMfcApp::Run () {int result = QMfcApp::run (this); delete qApp Return result;}\ endcode\ sa run () * /

First initialize the QApplication class in the InitInstance function of the app class

BOOL CMFCWithQtApp::InitInstance () {CWinApp::InitInstance (); QMfcApp::instance (this); return true;}

Then you need to override the run method of the app class, where you call the run method of QMFC

Int CMFCWithQtApp::Run () {int result = QMfcApp::run (this); delete qApp; return result;}

Compile again, and complete the function of adding signal slot mechanism to mfc.

Add qt interface

Create a new interface class in the project and let it inherit from QWidget, as follows

/ / MainUI.h#pragma once#include class MainUI: public QWidget {Q_OBJECTpublic: MainUI (QWidget* parent = nullptr); ~ MainUI ();}; / / MainUI.cpp#include "pch.h" # include "MainUI.h" # include MainUI::MainUI (QWidget* parent): QWidget (parent) {setWindowTitle ("Qt Windows"); setFixedSize (800720) QPushButton* pBtn = new QPushButton (QString::fromLocal8Bit ("this is a Qt button"), this);} MainUI::~MainUI () {}

Then start the interface in the InitInstance of the App class

BOOL CMFCWithQtApp::InitInstance () {CWinApp::InitInstance (); QMfcApp::instance (this); MainUI ui; ui.show (); QMfcApp::exec (); return FALSE;}

Then compile, at this time, I will find some errors when linking, and I can't find some definitions of meta functions.

Configuration meta-compilation process

The process from the source code to the generation of executable files requires pre-compilation, compilation, and linking. Qt will do meta-compilation before pre-compilation to generate a source file at the beginning of moc_, and the real file that will be compiled later is actually the file generated by this meta-compilation. The MFC project will not go through this step, so it will report an error.

Right-click on MainUI.h, select Properties, and select the project type as a custom build tool

Then apply, and new options will appear at this time.

Fill in "moc.exe"% (FullPath) "- o".\ GeneratedFiles\ moc_% (Filename) .CPP "- fpch.h"- f../MainUI.h" and.\ GeneratedFiles\ moc_% (Filename). Cpp in the command line and input fields, respectively.

The command line means that the current file is compiled using the moc meta compiler, and the generated file is placed in the GeneratedFiles subdirectory under the current directory, starting with moc_ as the file name, followed by-f indicating that the new file generated will contain # include "pch.h" and # include ".. / MainUI.h".

Then right-click in the file to compile. This will generate a moc file (double quotation marks on both sides can also be included)

If an error such as moc.exe is not reported during compilation, configure the bin path in QT to the environment variable

We add the newly generated file to the project

Compile again, and click run after success to see that the qt interface has been displayed.

The treatment of some problems

The window came out, but there were two problems in my environment, the process could not exit after closing the window, and the memory leak occurred after the program exited.

To solve these two problems, you can modify them in the QMfcApp.cpp file.

/ / means to close QApplicationsetQuitOnLastWindowClosed (true) when the last qt window exits; / / change the previous false to true// ~ QMfcApp () to add these two sentences, and close the process HANDLE hself = GetCurrentProcess (); TerminateProcess (hself, 0) when the destructing is complete; test signal slot

We can add signal slots to MainUI.

MainUI::MainUI (QWidget* parent): QWidget (parent) {setWindowTitle ("Qt Windows"); setFixedSize (800,720); QPushButton* pBtn = new QPushButton ("this is a Qt button"), this) Connect (pBtn, & QPushButton::clicked, [=] () {QMessageBox::information (this, QString::fromLocal8Bit ("signal slot"), QString::fromLocal8Bit ("this is popping up from the signal slot");});}

After clicking the button, the message box pops up normally.

Use qt designer to design interface

Use qtdesigner to design such an interface

Qtdesigner generates a .ui file, which is automatically generated into a corresponding .h file using uic.exe in the qt development environment. Let's first import the ui file into the project, and follow the previous steps to set up the custom build tool, filling in the following command line

"uic.exe"% (FullPath)"-o ".\ ui_% (Filename) .h"

And fill in the output path.\ ui_% (Filename) .h

After compilation, a corresponding ui_MainUi.h file is generated, the corresponding MainUI.h header file is modified, a reference to it is added, and an object pointer to ui is added

/ / MainUI.h#pragma once#include # include "ui_Main.h" class MainUI: public QWidget {Q_OBJECTpublic: MainUI (QWidget* parent = nullptr); ~ MainUI (); private: Ui::mainUI* masked PUI;}

In the construction of a class, ui objects are used to generate interface elements

/ / MainUI.cpp#include "pch.h" # include "MainUI.h" # include # include MainUI::MainUI (QWidget* parent): QWidget (parent), m_pUI (new Ui::mainUI ()) {masked UI-> setupUi (this) Connect (masked PUI-> pushButton, & QPushButton::clicked, [=] () {QMessageBox::information (this, QString::fromLocal8Bit ("qt slot Test"), massipUI-> lineEdit- > text ();});} MainUI::~MainUI () {delete massipUI;}

The execution effect is as follows

After reading the above, have you mastered the steps of using QT to develop the interface in MFC programs? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report