提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
在QT当中,遇到主界面和多个组件槽函数绑定问题时,为了便于管理,我们可以通过单例类作为第三方来进行管理。
提示:以下是本篇文章正文内容,下面案例可供参考
一、创建单例类
SignalInstance.h
#include <QObject>
class SignalInstance:public QObject
{
Q_OBJECT
public:
static SignalInstance *GetInstance();
static void Release();//释放
static SignalInstance *signalinstance;
signals:
void send_to_windwostwo();
void send_to_windwosone();
private:
SignalInstance();
};
SignalInstance.cpp
```cpp
#include "SignalInstance.h"
SignalInstance* SignalInstance::signalinstance = nullptr;//初始化对象
//释放单例对象
void SignalInstance::Release()
{
if (signalinstance != NULL)
{
delete signalinstance;
signalinstance = NULL;
}
}
SignalInstance::SignalInstance()
{
}
//获得单例对象
SignalInstance* SignalInstance::GetInstance()
{
if (signalinstance == NULL)
{
signalinstance = new SignalInstance();
}
return signalinstance;
}
二、主界面添加组件
代码如下(示例):
#include <QObject>
#include<QVBoxLayout>
#include <QtWidgets/QWidget>
#include "SignalInstance.h"
#include"QtWidgets_1.h"
#include"QtWidgetsClass_2.h"
#include "instanse.h"
instanse::instanse(QMainWindow *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QVBoxLayout *lay = new QVBoxLayout(this);
QtWidgets_1 *widget1 = new QtWidgets_1();
lay->addWidget(widget1);
ui.widget->setLayout(lay);
QVBoxLayout *lay2 = new QVBoxLayout(this);
QtWidgetsClass_2 *widget2 = new QtWidgetsClass_2();
lay2->addWidget(widget2);
ui.widget_2->setLayout(lay2);
}
三、组件代码绑定信号和槽
1.组件1
#pragma once
#include <QWidget>
#include "ui_QtWidgets_1.h"
class QtWidgets_1 : public QWidget
{
Q_OBJECT
public:
QtWidgets_1(QWidget *parent = Q_NULLPTR);
~QtWidgets_1();
private slots:
void Show();
private:
Ui::QtWidgets_1 ui;
};
#include "QtWidgets_1.h"
#include"SignalInstance.h"
#include <QObject>
#include <QtWidgets/QWidget>
#pragma execution_character_set("utf-8")
QtWidgets_1::QtWidgets_1(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), SignalInstance::GetInstance(), SIGNAL(send_to_windwostwo()));//绑定信号
connect(SignalInstance::GetInstance(), SIGNAL(send_to_windwosone()), this, SLOT(Show()));//绑定槽函数
}
QtWidgets_1::~QtWidgets_1()
{
}
void QtWidgets_1::Show()
{
ui.textEdit->setText("我是窗口2激活的");
}
2.组件2
#pragma once
#include <QWidget>
#include "ui_QtWidgetsClass_2.h"
class QtWidgetsClass_2 : public QWidget
{
Q_OBJECT
public:
QtWidgetsClass_2(QWidget *parent = Q_NULLPTR);
~QtWidgetsClass_2();
private slots:
void Show();
private:
Ui::QtWidgetsClass_2 ui;
};
#include "QtWidgetsClass_2.h"
#include"SignalInstance.h"
#include <QObject>
#include <QtWidgets/QWidget>
#pragma execution_character_set("utf-8")
QtWidgetsClass_2::QtWidgetsClass_2(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(SignalInstance::GetInstance(), SIGNAL(send_to_windwostwo()),this,SLOT(Show()));//绑定槽函数
connect(ui.pushButton, SIGNAL(clicked()), SignalInstance::GetInstance(), SIGNAL(send_to_windwosone()));//绑定信号
}
QtWidgetsClass_2::~QtWidgetsClass_2()
{
}
void QtWidgetsClass_2::Show()
{
ui.textEdit->setText("我是窗口1激活的");
}
四、效果图
下载地址:https://download.csdn.net/download/qq_38491692/85309274
总结
本文案例使用组件较少,当项目较大时,比如上千个控件和布局,能有效的对信号和槽进行管理。如果对您有帮助,请点个赞。