代码拉取完成,页面将自动刷新
#ifndef MSGMANAGER_H
#define MSGMANAGER_H
#include <QQueue>
#include <QVariant>
#include <QMutex>
#include <QObject>
#define MSG_MANAGER MessageManager::GetInstance()
/*1、及时通知
*
*
*
*
*/
// 观察者
class Observer
{
public:
Observer(){
};
~Observer(){
};
public:
// 消息处理虚函数
virtual void handleMessage(QVariant data) = 0;
};
/*-----------------------------------------------------------------
* @类名 MessageManager
* @说明 消息管理类
* @作者 hyy
* @日期 2021/07/28
------------------------------------------------------------------*/
class MessageManager : public QObject
{
Q_OBJECT
public:
static MessageManager *GetInstance(){
static MessageManager m;
return &m;
}
public:
//外部消息类型
enum MsgTypeOutside{
Outside_1,
Outside_2,
Outside_3,
Outside_4,
MsgTypeOutside_num
};
//内部消息类型(带参和不带参)
enum MsgTypeInside{
INSIDE_1 = MsgTypeOutside_num + 1, // 使两个枚举类型的范围不一样,方便判断
INSIDE_2,
INSIDE_3,
INSIDE_4,
};
//接口
public:
//注册及其处理函数函数
void Register(MsgTypeOutside msgType, Observer *obj);
void Register(MsgTypeInside msgType, Observer *obj);
// 将外部消息放入队列中
void PutOutsideMessage(MsgTypeOutside msgType, QVariant data);
// 将内部消息放入队列中
void PutInsideMessage(MsgTypeInside msgType, QVariant data);
signals:
void MessageSignal(int msgType, QVariant data);
private:
//构造函数私有化
MessageManager(){
connect(this, &MessageManager::MessageSignal, this, &MessageManager::handleMessage);
};
void registerMsg(int msgType, Observer *obj);;
//消息处理函数
void handleMessage(int msgType, QVariant data);
private:
//消息来源类型
enum MsgSourceType{
MSG_OUTSIDE,
MSG_INSIDE
};
struct Message{
MsgSourceType sourceType; // 消息源类型
int msgType; // 具体的消息类型
QVariant data; // 数据
Message(){
};
Message(MsgSourceType source, int msgType, QVariant data) :
sourceType(source),
msgType(msgType),
data(data)
{
};
};
private:
QMap<int, QList<Observer *>> mObeservers;
QQueue<Message> mMsgList;
// 消息处理互斥锁
QMutex mLock;
};
#endif // MSGMANAGER_H
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。