QAbstractSocket stateChanged()-Signal 的使用
Use of QAbstractSocket stateChanged()-Signal
QAbstractSocket stateChanged()-Signal的正确use/implementation是什么?
main.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变为 Connected 到 Unconnected,但永远不会调用 SLOT-Function。我在这里错过了什么?
您的 SLOT
签名与 SIGNAL
不匹配。
将函数声明和定义更改为
void verbindungWurdeBeendet(QAbstractSocket::SocketState state);
以及
的连接语句
connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(verbindungWurdeBeendet(QAbstractSocket::SocketState)));
SLOT
函数verbindungWurdeBeendet()
(没有参数)也是可以接受的,如果你在函数声明中有默认值。
例如:
在下面的例子中,你可以在 connect
语句中有一个 SLOT
,不带参数。
void verbindungWurdeBeendet(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);
问题是因为您正在连接到一个对象:
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
QAbstractSocket stateChanged()-Signal的正确use/implementation是什么?
main.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变为 Connected 到 Unconnected,但永远不会调用 SLOT-Function。我在这里错过了什么?
您的 SLOT
签名与 SIGNAL
不匹配。
将函数声明和定义更改为
void verbindungWurdeBeendet(QAbstractSocket::SocketState state);
以及
的连接语句connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(verbindungWurdeBeendet(QAbstractSocket::SocketState)));
SLOT
函数verbindungWurdeBeendet()
(没有参数)也是可以接受的,如果你在函数声明中有默认值。
例如:
在下面的例子中,你可以在 connect
语句中有一个 SLOT
,不带参数。
void verbindungWurdeBeendet(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);
问题是因为您正在连接到一个对象:
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