企业级多人开发框架(多界面websocket通信) 完整版Qt5.9 + MSVC2015_64企业级多人开发框架(可直接复制编译运行)包含:6 个模块 DLL + 主程序 + WebSocket 测试服务端,全套完整无缺失直接复制 → 新建文件 → 编译 → 运行一、总工程EnterpriseDLL.proqmakeTEMPLATE = subdirs CONFIG += ordered SUBDIRS += \ CoreLib \ CommLib \ DataLib \ ModelLib \ ModulesLib \ MainApp MainApp.depends = ModulesLib ModulesLib.depends = ModelLib DataLib CoreLib ModelLib.depends = CoreLib DataLib.depends = CoreLib CommLib CommLib.depends = CoreLib二、CoreLib 模块CoreLib.proqmakeQT += core gui widgets TEMPLATE = lib CONFIG += dynamiclib DEFINES += CORELIB_LIBRARY DESTDIR = $$PWD/../bin INCLUDEPATH += $$PWD CONFIG += c++11 HEADERS += \ CoreLib_global.h \ GlobalDefine.h \ EventBus.h \ WindowManager.h SOURCES += \ EventBus.cpp \ WindowManager.cppCoreLib_global.hcpp运行#ifndef CORELIB_GLOBAL_H #define CORELIB_GLOBAL_H #include QtGlobal #if defined(CORELIB_LIBRARY) # define CORELIB_EXPORT Q_DECL_EXPORT #else # define CORELIB_EXPORT Q_DECL_IMPORT #endif #endifGlobalDefine.hcpp运行#ifndef GLOBALDEFINE_H #define GLOBALDEFINE_H #include "CoreLib_global.h" enum DataType { TYPE_DEVICE = 1, TYPE_ALARM, TYPE_CURVE }; #endifEventBus.hcpp运行#ifndef EVENTBUS_H #define EVENTBUS_H #include "CoreLib_global.h" #include QObject #include QVariant class CORELIB_EXPORT EventBus : public QObject { Q_OBJECT public: static EventBus* instance(); void post(int type, const QVariant data); signals: void sigEvent(int type, QVariant data); private: static EventBus* inst; explicit EventBus(QObject *parent = 0); }; #endifEventBus.cppcpp运行#include "EventBus.h" EventBus* EventBus::inst = 0; EventBus* EventBus::instance() { if (!inst) inst = new EventBus; return inst; } EventBus::EventBus(QObject *parent) : QObject(parent) { } void EventBus::post(int type, const QVariant data) { emit sigEvent(type, data); }WindowManager.hcpp运行#ifndef WINDOWMANAGER_H #define WINDOWMANAGER_H #include "CoreLib_global.h" #include QWidget #include QMap class CORELIB_EXPORT WindowManager { public: static WindowManager* instance(); void reg(const QString name, QWidget* w); void show(const QString name); private: static WindowManager* inst; QMapQString, QWidget* m_map; WindowManager(); }; #endifWindowManager.cppcpp运行#include "WindowManager.h" WindowManager* WindowManager::inst = 0; WindowManager* WindowManager::instance() { if (!inst) inst = new WindowManager; return inst; } WindowManager::WindowManager() { } void WindowManager::reg(const QString name, QWidget *w) { m_map[name] = w; } void WindowManager::show(const QString name) { if (m_map.contains(name)) m_map[name]-show(); }三、CommLib 模块CommLib.proqmakeQT += core network websockets TEMPLATE = lib CONFIG += dynamiclib DEFINES += COMMLIB_LIBRARY DESTDIR = $$PWD/../bin INCLUDEPATH += $$PWD $$PWD/../CoreLib LIBS += -L$$PWD/../bin -lCoreLib CONFIG += c++11 HEADERS += \ CommLib_global.h \ DataQueue.h \ WebSocketClient.h SOURCES += \ WebSocketClient.cppCommLib_global.hcpp运行#ifndef COMMLIB_GLOBAL_H #define COMMLIB_GLOBAL_H #include QtGlobal #if defined(COMMLIB_LIBRARY) # define COMMLIB_EXPORT Q_DECL_EXPORT #else # define COMMLIB_EXPORT Q_DECL_IMPORT #endif #endifDataQueue.hcpp运行#ifndef DATAQUEUE_H #define DATAQUEUE_H #include QQueue #include QMutex templatetypename T class DataQueue { public: void enqueue(const T t) { m_mutex.lock(); m_queue.enqueue(t); m_mutex.unlock(); } bool dequeue(T t) { if (m_queue.isEmpty()) return false; m_mutex.lock(); t = m_queue.dequeue(); m_mutex.unlock(); return true; } private: QQueueT m_queue; QMutex m_mutex; }; #endifWebSocketClient.hcpp运行#ifndef WEBSOCKETCLIENT_H #define WEBSO