QStackedWidget动态多界面

1、新建子界面的虚基类

uiinterface.h

#ifndef UIINTERFACE_H
#define UIINTERFACE_H
#include <QWidget>
namespace UIINTERFACE_NAME_SPACE {
#define UI_WIDTH 1520
#define UI_HEIGHT 1118
}

class UiInterface : public QWidget
{
    Q_OBJECT
public:
    UiInterface(QWidget *parent = 0);
private:
    /*初始化*/
    virtual void _init(){}
};

#endif // UIINTERFACE_H

uiinterface.cpp

#include "uiinterface.h"
using namespace UIINTERFACE_NAME_SPACE;
UiInterface::UiInterface(QWidget *parent)
    :QWidget(parent)
{this->resize(UI_WIDTH, UI_HEIGHT);
}

2、新建子界面,类型是Qt设计师界面类,然后稍作修改【多个】

uiradarperform.h

#ifndef UIRADARPERFORM_H
#define UIRADARPERFORM_H

#include "uiinterface.h"

namespace Ui {
class UiRadarPerform;
}

class UiRadarPerform : public UiInterface
{
    Q_OBJECT

public:
    explicit UiRadarPerform(QWidget *parent = 0);
    ~UiRadarPerform();private:
    Ui::UiRadarPerform *ui;
    virtual void _init() override;
};

#endif // UIRADARPERFORM_H

 

uiradarperform.cpp

#include "uiradarperform.h"
#include "ui_uiradarperform.h"

UiRadarPerform::UiRadarPerform(QWidget *parent) :
    UiInterface(parent),
    ui(new Ui::UiRadarPerform)
{
    ui->setupUi(this);
    (void)_init();
}

UiRadarPerform::~UiRadarPerform()
{
    delete ui;
}


uiradarperform.ui

可自定义界面

3、在mainwindow.ui里拖入一个QStackedWidget

4、在mainwindow.cpp构造函数里

UiRadarPerform* pUI = new UiRadarPerform;
ui->stackedWidget->addWidget(pUI);

 

 

ps:重点和难点就是对Qt设计师界面类的修改,默认是继承QWidget,改成继承UiInterface类

posted @ 2021-02-23 23:30  朱小勇  阅读(312)  评论(0编辑  收藏  举报