Why in class QTcpServer slot incomingConnection that is override virtual not works?
-
Why in class QTcpServer slot incomingConnection that overrides virtual not works?
I used this tutorial( Multithread Client/Server) to create qtcpsocket. But when I send a message from the client to the server the connection is not starting... I expect when a connection is received in a different thread qtcpsocket is connected and prints the message of the client.
Thanks in advance// myserver.h #ifndef MYSERVER_H #define MYSERVER_H #include <QTcpServer> #include <QDebug> #include "mythread.h" class MyServer : public QTcpServer { Q_OBJECT public: explicit MyServer(QObject *parent = 0); void StartServer(); signals: public slots: protected: void incomingConnection(int socketDescriptor); }; #endif // MYSERVER_H
// myserver.cpp #include "myserver.h" MyServer::MyServer(QObject *parent) : QTcpServer(parent) { } void MyServer::StartServer() { if(!this->listen(QHostAddress::Any,1234)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening..."; } } void MyServer::incomingConnection(int socketDescriptor) { qDebug() << socketDescriptor << " Connecting..."; MyThread *thread = new MyThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }
-
Finally, I change the definition and declaration of incomingConnection slot according to below now everything is ok...
#ifndef MYSERVER_H #define MYSERVER_H #include <QTcpServer> #include <QDebug> #include "mythread.h" class MyServer : public QTcpServer { Q_OBJECT public: explicit MyServer(QObject *parent = 0); void StartServer(); signals: public slots: protected: void incomingConnection(qint64 socketDescriptor); }; #endif // MYSERVER_H
#include "myserver.h" MyServer::MyServer(QObject *parent) : QTcpServer(parent) { } void MyServer::StartServer() { if(!this->listen(QHostAddress::Any,1234)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening..."; } } void MyServer::incomingConnection(qint64 socketDescriptor) { qDebug() << socketDescriptor << " Connecting..."; MyThread *thread = new MyThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }
-
@stackprogramer please, when you override something, use the override keyword! the compiler would have told you, that you're trying the override a function, that does not exist/is not virtual.
The function declaration is:
void QTcpServer::incomingConnection(qintptr)
which is clearly different from your
MyServer::incomingConnection(int)
-
I have downloaded this example from the site I mentioned in the link. With these interpretations, what changes should I make in the code in order for data to arrive and a new thread to be executed?
I saw this tutorial, override base function is the same as it is done in up example...
https://www.programiz.com/cpp-programming/function-overriding -
void incomingConnection(qintptr socketDescriptor) override;
the original creator did it "wrong" as well, no override mark, and int instead of qintptr, which was probably fine for him at the time.
-
Finally, I change the definition and declaration of incomingConnection slot according to below now everything is ok...
#ifndef MYSERVER_H #define MYSERVER_H #include <QTcpServer> #include <QDebug> #include "mythread.h" class MyServer : public QTcpServer { Q_OBJECT public: explicit MyServer(QObject *parent = 0); void StartServer(); signals: public slots: protected: void incomingConnection(qint64 socketDescriptor); }; #endif // MYSERVER_H
#include "myserver.h" MyServer::MyServer(QObject *parent) : QTcpServer(parent) { } void MyServer::StartServer() { if(!this->listen(QHostAddress::Any,1234)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening..."; } } void MyServer::incomingConnection(qint64 socketDescriptor) { qDebug() << socketDescriptor << " Connecting..."; MyThread *thread = new MyThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }