data_package.h 文件

  #ifndef DATA_PACKAGE_H
  #define DATA_PACKAGE_H

  #include <QObject>

  class DataPackage : public QObject
  {
      Q_OBJECT
  public:          
      static DataPackage &instance();

  signals:

  public slots:

  private:
      explicit DataPackage(QObject *parent = 0);

  };

  #endif // DATA_PACKAGE_H

data_package.cpp 文件

  #include "data_package.h"
  #include <QDebug>

  DataPackage::DataPackage(QObject *parent) :
      QObject(parent)
  {
      qDebug() << "init data package";
  }

  DataPackage& DataPackage::instance()
  {
      static DataPackage _instance;
      return _instance;
  }

调用:

  DataPackage::instance();
  DataPackage::instance();

连续调用两次 instance 方法,发现只会示例一次。搞定。

参考资料

C++ Singleton design pattern