Hello everyone.
I have been assigned with the responsibility of maintaining some old Qt program.
In the process i had to convert it from Qt 4 to Qt 5.10 and from Qwt 4.x.x to Qwt 6.1.3
Everything seemed to work and i can compile and run the program.
However, when the server is set up to listen on the corresponding ports, new_Connection() is never called.
I get the following warning:
More specifically (see code below) the lineQuote:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is gui::model::Listener(0x2ba442c0), parent's thread is QThread(0x215f97b8), current thread is QThread(0x2ba445b0)
is causing it.
I have a strong feeling that it's becauseis not in the same thread asCode:
Listener
Code:
server_
I have search the internet thin, but I cannot seem to find any post that has relevance (or they are too old)
Code:
#include <QtNetwork> #include <QtDebug> #include "listener.h" #include "sockethandler.h" #include <qthread.h> // Assertion to ensure that char and short has the expected sizes // (required for protocol to work) namespace gui { namespace model { { } /** * Create server and listen for connections */ void Listener::listen() { if (server_!=0) qFatal("listen() called on a connection that was already initialized"); qDebug() << connect(server_, SIGNAL(newConnection()), this, SLOT(new_Connection())); emit log(tr("Listening on port %1").arg(port_)); } /** * Called on new connection. * Check for duplicate and initialize socket. */ void Listener::new_Connection() { while (server_->hasPendingConnections()) { emit log(tr("Connection opened on port %1").arg(port_)); emit connected( new SocketHandler(this, server_->nextPendingConnection()) ); } } }} // gui::model
Writing instead of removes the warning, but the problem persists.
.h file:
Code:
#ifndef CONNECTION_H #define CONNECTION_H #include <QObject> #include "connectassert.h" QT_BEGIN_NAMESPACE class QTcpServer; class QTcpSocket; QT_END_NAMESPACE namespace gui { namespace model { class SocketHandler; /** * Listens on a single port for a connection from HAsim. */ Q_OBJECT public: /** Creates object */ /** Listens for connections */ void listen(); signals: /** Emitted when a connection is opened */ void connected(SocketHandler*); /** Emitted when there is information available about connection */ private slots: void new_Connection(); private: int port_; QTcpServer *server_; }; }} // gui::model #endif // CONNECTION_H
So how can I correctly fix the error?
EDIT: I forgot to add - This worked before (on Qt4), so what is changed from Qt4 to Qt5.10 that would cause this to fail?
I hope you can help
best regards
David