我创建了一个名为CSocket的类:

CSocket.h

#ifndef CSOCKET_H
#define CSOCKET_H

#include <QtCore/QObject>
#include <QtWebSockets/QWebSocket>

class CSocket : public QObject
{
    Q_OBJECT
public:
    explicit CSocket(QObject *parent = nullptr);
    void onConnect(const QUrl &url);
    void onSendMesssage(QString message);
signals:
    void closed();
private slots:
    void onConnected();
    void onTextMessageReceived(QString message);
private:
    QWebSocket m_webSocket;
    QUrl m_url;
};

#endif // CSOCKET_H

CSocket.cpp
#include "csocket.h"

#include <QtCore/QDebug>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>

QT_USE_NAMESPACE

CSocket::CSocket(QObject *parent) : QObject(parent)
{
}

void CSocket::onConnect(const QUrl &url)
{
    m_url = url;

    connect(&m_webSocket, &QWebSocket::connected, this, &CSocket::onConnected);
    connect(&m_webSocket, &QWebSocket::disconnected, this, &CSocket::closed);

    m_webSocket.open(QUrl(url));
}

void CSocket::onConnected()
{
    connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &CSocket::onTextMessageReceived);
}

void CSocket::onTextMessageReceived(QString message)
{
    QMessageBox::information(nullptr, "Answer", message, QMessageBox::Ok);
}

void CSocket::onSendMesssage(QString message)
{
    m_webSocket.sendTextMessage(message);
}

在主窗口(QWidget)中,我创建一个连接:
CSocket *socket = new CSocket;
socket->onConnect(QUrl(QStringLiteral("ws://localhost:8080")));

现在的问题是:如何共享与另一个QWidget或QDialog的连接?我只是不想在新窗口中重新连接。有人知道怎么做吗?

最佳答案

假设在您的整个应用程序中,您只希望按照指示进行连接,则合适的模式将是单例:

csocket.h

#ifndef CSOCKET_H
#define CSOCKET_H

#include <QObject>
#include <QWebSocket>

class CSocket : public QObject
{
    Q_OBJECT
public:
    static CSocket *instance();
    void onConnect(const QUrl &url);
    void onSendMesssage(QString message);
signals:
    void closed();
private slots:
    void onConnected();
    void onTextMessageReceived(QString message);
private:
    static CSocket* m_instance;
    explicit CSocket(QObject *parent = nullptr);
    QWebSocket m_webSocket;
    QUrl m_url;
};

#endif // CSOCKET_H

csocket.cpp
#include "csocket.h"

#include <QMessageBox>

CSocket* CSocket::m_instance = nullptr;

CSocket::CSocket(QObject *parent) : QObject(parent)
{
}

CSocket *CSocket::instance()
{
    if (m_instance == nullptr)
        m_instance = new CSocket;
    return m_instance;
}

void CSocket::onConnect(const QUrl &url)
{
    m_url = url;

    connect(&m_webSocket, &QWebSocket::connected, this, &CSocket::onConnected);
    connect(&m_webSocket, &QWebSocket::disconnected, this, &CSocket::closed);

    m_webSocket.open(QUrl(url));
}

void CSocket::onConnected()
{
    connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &CSocket::onTextMessageReceived);
}

void CSocket::onTextMessageReceived(QString message)
{
    QMessageBox::information(nullptr, "Answer", message, QMessageBox::Ok);
}

void CSocket::onSendMesssage(QString message)
{
    m_webSocket.sendTextMessage(message);
}

因此,应该使用instance()方法而不是使用构造函数:
//mainwindow

CSocket *socket = CSocket::instance();
socket->onConnect(QUrl(QStringLiteral("ws://localhost:8080")));
// another window
CSocket *socket = CSocket::instance();

如您所见,所有窗口都将共享一个CSocket。

09-27 17:29