mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QMutex>
#include <memory>
#include <QPushButton>
class SubWidget;
class MainWidget : public QWidget
{
Q_OBJECT
private:
MainWidget(QWidget *parent = nullptr);
MainWidget(const MainWidget&);
MainWidget operator=(const MainWidget&);
private:
static std::shared_ptr<MainWidget> mainWidgetInst;
static QMutex mutex;
public:
static std::shared_ptr<MainWidget> getInstance();
//
~MainWidget();
private:
QPushButton *btn;
std::shared_ptr<SubWidget> instSub;
public slots:
void slot_btn_Clicked();
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include "mainwidget.h"
#include "subwidget.h"
QMutex MainWidget::mutex;
std::shared_ptr<MainWidget> MainWidget::mainWidgetInst;
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
btn = new QPushButton(this);
btn->setGeometry(100, 100, 80, 40);
btn->setText(QString("Main"));
connect(btn, SIGNAL(pressed()), this, SLOT(slot_btn_Clicked()));
}
MainWidget::~MainWidget()
{
}
std::shared_ptr<MainWidget> MainWidget::getInstance()
{
if(nullptr == mainWidgetInst)
{
mutex.lock();
if(nullptr == mainWidgetInst)
{
mainWidgetInst = std::shared_ptr<MainWidget>(new MainWidget());
}
mutex.unlock();
}
return mainWidgetInst;
}
void MainWidget::slot_btn_Clicked()
{
instSub = SubWidget::getInstance();
instSub->show();
this->close();
}
subwidget.h
#ifndef SUBWIDGET_H
#define SUBWIDGET_H
#include <QWidget>
#include <QMutex>
#include <memory>
#include <QPushButton>
class MainWidget;
class SubWidget : public QWidget
{
Q_OBJECT
private:
SubWidget(QWidget *parent = nullptr);
SubWidget(const SubWidget&);
SubWidget operator=(const SubWidget&);
private:
static std::shared_ptr<SubWidget> subWidgetInst;
static QMutex mutex;
public:
static std::shared_ptr<SubWidget> getInstance();
~SubWidget();
private:
QPushButton *btn;
QPushButton *btnOK;
std::shared_ptr<MainWidget> instMain;
signals:
public slots:
void slot_btn_Clicked();
void slot_btnOK_Clicked();
};
#endif // SUBWIDGET_H
subwidget.cpp
#include "subwidget.h"
#include "mainwidget.h"
QMutex SubWidget::mutex;
std::shared_ptr<SubWidget> SubWidget::subWidgetInst;
SubWidget::SubWidget(QWidget *parent) : QWidget(parent)
{
btn = new QPushButton(this);
btn->setGeometry(100, 100, 80, 40);
btn->setText(QString("Sub"));
connect(btn, SIGNAL(pressed()), this, SLOT(slot_btn_Clicked()));
//
btnOK = new QPushButton(this);
btnOK->setGeometry(200,100,80,40);
connect(btnOK, SIGNAL(pressed()), this, SLOT(slot_btnOK_Clicked()));
}
SubWidget::~SubWidget()
{
;
}
std::shared_ptr<SubWidget> SubWidget::getInstance()
{
if(nullptr == subWidgetInst)
{
mutex.lock();
if(nullptr == subWidgetInst)
{
subWidgetInst = std::shared_ptr<SubWidget>(new SubWidget());
}
mutex.unlock();
}
return subWidgetInst;
}
void SubWidget::slot_btn_Clicked()
{
instMain = MainWidget::getInstance();
instMain->show();
this->close();
}
void SubWidget::slot_btnOK_Clicked()
{
int t = 0;
t++;
}
main.cpp
#include "mainwidget.h"
#include <memory>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::shared_ptr<MainWidget> inst = MainWidget::getInstance();
inst->show();
return a.exec();
}
1、类相互引用
在类A的头文件中,使用类B的前置声明,不引用类B的头文件;
在类A的源文件中引用类B的头文件。
类B中同理。
2、智能指针
需要C++14支持。