52 lines
729 B
C++
52 lines
729 B
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <QList>
|
|
template <typename T>
|
|
class AutoDeleteList
|
|
{
|
|
public:
|
|
// 添加对象
|
|
void add(T *obj)
|
|
{
|
|
objs.append(obj);
|
|
}
|
|
|
|
// 析构对象列表
|
|
~AutoDeleteList()
|
|
{
|
|
qDeleteAll(objs);
|
|
}
|
|
|
|
private:
|
|
QList<T *> objs;
|
|
};
|
|
|
|
|
|
class ExitManager : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
// 添加需要在退出时自动析构的对象
|
|
template <typename T>
|
|
void add(T *obj)
|
|
{
|
|
autoDeleteList.add(obj);
|
|
}
|
|
|
|
// 退出应用程序
|
|
void exit(int exitCode = 0)
|
|
{
|
|
|
|
// 发射退出应用程序信号
|
|
emit exitApplication(exitCode);
|
|
}
|
|
|
|
signals:
|
|
// 退出应用程序信号
|
|
void exitApplication(int exitCode);
|
|
|
|
private:
|
|
// 自动析构对象列表
|
|
AutoDeleteList<QObject> autoDeleteList;
|
|
}; |