使用Qt 的时候发现:
于是开始了如下的测试:
我的代码有5个文件
cmapedit.h
/**
* @brief 自定义主窗口类
* @details 仅修改 paintEvent,通过宏定义实现动态编译
* @mainpage
* @date 2023-10-24
*/
#ifndef CMAPEDIT_H
#define CMAPEDIT_H
#include <QWidget>
#include <QMainWindow>
#include "ui_MapEditUi_MainWindow.h"
#include "ui_MapEditUi_Widget.h"
#define inherit_QMainWindow // 宏定义:继承自 QMainWindow 还是 QWidget
#define USE_QMainWindow // 宏定义:ui类是 QMainWindow 还是 QWidget
#ifdef inherit_QMainWindow
class CMapEdit : public QMainWindow
#else
class CMapEdit : public QWidget
#endif
{
Q_OBJECT
public:
CMapEdit(QWidget *parent = nullptr);
~CMapEdit();
void paintEvent(QPaintEvent *);
public:
static int m_iCount;
#ifdef USE_QMainWindow
Ui::MapEditUi_MainWindow *ui = nullptr;
#else
Ui::MapEditUi_Widget *ui = nullptr;
#endif
};
#endif // CMAPEDIT_H
cmapedit.cpp
#include "cmapedit.h"
#include <QDebug>
int CMapEdit::m_iCount = 1;
#ifdef inherit_QMainWindow
CMapEdit::CMapEdit(QWidget *parent): QMainWindow(parent)
#else
CMapEdit::CMapEdit(QWidget *parent): QWidget(parent)
# endif
{
#ifdef USE_QMainWindow
ui = new Ui::MapEditUi_MainWindow;
#else
ui = new Ui::MapEditUi_Widget;
# endif
ui->setupUi(this);
}
CMapEdit::~CMapEdit(){}
void CMapEdit::paintEvent(QPaintEvent * event)
{
qDebug()<< "paint" << m_iCount++;
}