c++ - QAbstractSocket stateChanged()-Signal 的使用

标签 c++ qt qt-signals qtcpsocket qt-slot

QAbstractSocket stateChanged()-Signal 的正确使用/实现是什么?

主要.cpp:

#include <QCoreApplication>

#include <sslserver.h>
#include <QLoggingCategory>

int main(int argc, char *argv[]){

QCoreApplication a(argc, argv);
QLoggingCategory::setFilterRules("*.debug=true");

SslServer *myTestServer = new SslServer();

return a.exec();
}

SslServer.h:

#ifndef SSLSERVER_H
#define SSLSERVER_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>

class SslServer : public QObject
{
    Q_OBJECT
public:
    explicit SslServer(QObject *parent = nullptr);

private slots:
    void newTestConnection();
    void sendTestdata();
    void connectionWasClosed(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);

private:
    QTcpServer *testServer;
    QTcpSocket *testSocket;
    QTimer *testTimer;
};

#endif // SSLSERVER_H

SslServer.cpp:

#include "sslserver.h"
#include <QDebug>

SslServer::SslServer(QObject *parent) : QObject(parent){

    testServer = new QTcpServer(this);
    testSocket = new QTcpSocket();
    testTimer = new QTimer();

    connect(testServer, SIGNAL(newConnection()), this, SLOT(newTestConnection()));  //works
    connect(testTimer, SIGNAL(timeout()), this, SLOT(sendTestdata()));              //works
    connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(connectionWasClosed(QAbstractSocket::SocketState)));
    //Qt5 Connection was also tested, without any change to old connection-typo
    //connect(testSocket, &QAbstractSocket::stateChanged, this, &SslServer::verbindungWurdeBeendet);

    testTimer->start(1000);

    if(testServer->listen(QHostAddress::Any, 9999)){
        qDebug() << "Server running on Port 9999";
    }else{
        qDebug() << "Error while building server";
    }

}

void SslServer::newTestConnection(){
    testSocket->close();
    testSocket = testServer->nextPendingConnection();
    testSocket->write("Welcome on: Testserver!");
    testSocket->flush();

}

void SslServer::sendTestdata(){
    if(testSocket->isOpen()){
        qDebug() << testSocket->state();
        qDebug() << "Sending testdata to client";
        testSocket->write("testdata");
    }
}

void SslServer::connectionWasClosed(QAbstractSocket::SocketState){

    qDebug() << "!!! SocketState changed !!!";

}

testSocket->state()语句可以看出状态正在从Unconnected变为ConnectedUnconnected ,但永远不会调用 SLOT-Function。我在这里缺少什么?

最佳答案

问题是因为你正在连接一个对象:

testSocket = new QTcpSocket();

但是您希望从另一个对象接收信号:

testSocket = testServer->nextPendingConnection();

解决方案是在收到新对象时建立连接:

void SslServer::newTestConnection(){
    if(testSocket)
        testSocket->close();
    testSocket = testServer->nextPendingConnection();
    connect(testSocket, &QAbstractSocket::stateChanged, this, &SslServer::connectionWasClosed);
    testSocket->write("Welcome on: Testserver!");
    testSocket->flush();
}

完整的例子可以在下面的link中找到

关于c++ - QAbstractSocket stateChanged()-Signal 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47400049/

相关文章:

c++ - 我如何在我的代码C++中解决此错误

qt - 如何在 QTableWidget 上显示数据并使用标题从中读取数据?

c++ - Qt 关闭上一个窗口

c++ - QObject连接函数

c++ - 如何正确使用 qt 的信号/插槽系统

c++ - C++ 头文件不包含任何其他头文件的任何充分理由?

c++ - 当使用 std::vector 的 emplace_back 或 push_back 创建时,在从仅 move 类型派生的类中定义析构函数会产生编译时错误

C++ 模板类复制构造函数和赋值运算符

python - 如何在 PyQt 中设置大于屏幕 2/3 的小部件的初始大小

c++ - 信号和插槽在 qt 中不起作用