singleton.h
#include <QObject>
#include <QMutex>
#include <QMutexLocker>
class Singleton:public QObject
{
public:
static Singleton* instance()
{
static QMutex mutex;
if (!m_instance) {
QMutexLocker locker(&mutex);
if (!m_instance)
m_instance = new Singleton;
}
return m_instance;
}
private:
Singleton();
static Singleton* m_instance;
};
singleton.cpp
#include <QtCore/QCoreApplication>
#include "singleton.h"
Singleton *Singleton::m_instance = 0;
Singleton::Singleton():
QObject(qApp)
{
}