Qt有自带的宏可以使用单例模式,Q_GLOBAL_STATIC通过这个宏不但定义简单,还可以获得线程安全性。
test01.h
#ifndef TEST001_H
#define TEST001_H
#include <QObject>
class Test001 : public QObject
{
Q_OBJECT
public:
static Test001* Get_Test001();
explicit Test001(QObject *parent = nullptr);
signals:
public slots:
private:
};
#endif // TEST001_H
test01.cpp
#include "test001.h"
Q_GLOBAL_STATIC(Test001,test001)
Test001* Test001::Get_Test001()
{
return test001();
}
Test001::Test001(QObject *parent) : QObject(parent)
{
}
调用例子:
Test001::Get_Test001();