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:
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)
More specifically (see code below) the line
server_ = new QTcpServer(this);
To copy to clipboard, switch view to plain text mode
is causing it.
I have a strong feeling that it's because
Listener
Listener
To copy to clipboard, switch view to plain text mode
is not in the same thread as
server_
server_
To copy to clipboard, switch view to plain text mode
I have search the internet thin, but I cannot seem to find any post that has relevance (or they are too old)
#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 {
Listener
::Listener(QObject *parent,
int port
) : QObject(parent
), port_
(port
), server_
(0) { }
/**
* 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
#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 {
Listener::Listener(QObject *parent, int port) :
QObject(parent), port_(port), server_(0)
{ }
/**
* Create server and listen for connections
*/
void Listener::listen() {
if (server_!=0) qFatal("listen() called on a connection that was already initialized");
server_ = new QTcpServer(this);
server_->listen(QHostAddress::Any, port_);
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
To copy to clipboard, switch view to plain text mode
Writing
server_ = new QTcpServer();
To copy to clipboard, switch view to plain text mode
instead of
server_ = new QTcpServer(this);
To copy to clipboard, switch view to plain text mode
removes the warning, but the problem persists.
.h file:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QObject>
#include "connectassert.h"
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
namespace gui { namespace model {
class SocketHandler;
/**
* Listens on a single port for a connection from HAsim.
*/
Q_OBJECT
public:
/** Creates object */
explicit Listener
(QObject *parent,
int port
);
/** 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_;
};
}} // gui::model
#endif // CONNECTION_H
#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.
*/
class Listener : public QObject {
Q_OBJECT
public:
/** Creates object */
explicit Listener(QObject *parent, int port);
/** Listens for connections */
void listen();
signals:
/** Emitted when a connection is opened */
void connected(SocketHandler*);
/** Emitted when there is information available about connection */
void log(QString str);
private slots:
void new_Connection();
private:
int port_;
QTcpServer *server_;
};
}} // gui::model
#endif // CONNECTION_H
To copy to clipboard, switch view to plain text mode
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
Bookmarks