Promote QWidget to QMainWindow in QT

Introduction

The background is that a piece of software has been developed, and the base class uses QWidget. With the increase of software functions, QWidget can no longer accommodate too many components. Therefore, it is necessary to switch to QMainWindow to use menus and toolboxes to accommodate more functions.

Project introduction: The UI is generated and laid out by dragging and dropping components using Qt designers. The following records how to convert this type of project into the QMainWindow base class. Here is the simplest and most effective way.

step

Create a new QMainWindow base class window

Open Qt Designer to create a new QMainWindow base class window.
insert image description here
Change the default class name MainWindow to your own class name. The red box in the lower right corner of the figure below is the class name of the QWidget base class in my project. Being consistent here will reduce the amount of subsequent code modifications. After changing the class name, save it, and just choose a suitable file name yourself.
insert image description here
Open the UI file of the QWidget base class, select all components and copy them to the newly created QMainWindow base class UI file. As shown in the figure below, the above UI is a UI file based on QMainWindow.

insert image description here
Import the above modified UI file into VS2013, if it is Qt Creator, it should be able to load directly. After importing, the corresponding header files will be automatically generated.

modify the code

The code that needs to be modified, add the header file

#include <QtWidgets/QMainWindow>		 //增加QMainWindow头文件
#include "ui_dinirawdatasimulation_mw.h" //自己UI文件导入后新生成的头文件

insert image description here

Change the QWidget in the figure below to QMainWindow
insert image description here
Change the QWidget in the figure below to QMainWindow
insert image description here

epilogue

After the above steps, recompile the project on it. Then you can further modify the new window and debug the function.

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/127464162