QT 单例模式

single.h

class Single
{
public:
    static Single *Instance();
    explicit Single (QWidget *parent = 0);
    ~Single ();

protected:
    void showEvent(QShowEvent *);

private:
    Ui::frmSingle *ui;
    static QScopedPointer self;
};

#include "single.h"

QScopedPointer Single::self;
Single *Single::Instance()
{
    if (self.isNull()) {
        static QMutex mutex;
        QMutexLocker locker(&mutex);
        if (self.isNull()) {
            self.reset(new Single);
        }
    }

    return self.data();
}

Single::Single(QWidget *parent) : QDialog(parent), ui(new Ui::Single)
{
    ui->setupUi(this);
}

Single::~Single()
{
    delete ui;
}

你可能感兴趣的:(单例模式,ui)