Problem with QTcpSocket in QThread
When I have a QThread which is responsible for handling a connection for a client application, I seem to have a strange problem. When I allocate a QTcpSocket in the constructor of the QThread, I do not succeed to open a connection afterwards in run(). When I do the allocation in run(), no problems arises. Any idea what might cause this ?
Header file :
Code:
#ifndef CONNECTIONTHREAD_H
#define CONNECTIONTHREAD_H
#include <QThread>
#include <QString>
class ConnectionThread
: public QThread{
Q_OBJECT
public:
~ConnectionThread();
bool isConnected() const;
signals:
void connectionEstablished();
public slots:
//void setHostInfo(QString& hostName, int portNumber);
private:
void run();
bool connected_;
int portNumber_;
};
#endif // CONNECTIONTHREAD_H
This works :
Code:
#include "connectionthread.h"
#include <QTcpSocket>
ConnectionThread
::ConnectionThread(QObject *parent
){
connected_ = false;
hostName_ = "localhost";
portNumber_ = 4974;
connect(this, SIGNAL(connectionEstablished()), parent, SLOT(connectionEstablished()));
}
ConnectionThread::~ConnectionThread()
{
}
bool ConnectionThread::isConnected() const
{
return connected_;
}
void ConnectionThread::run()
{
tcpSocket_->abort();
tcpSocket_->connectToHost(hostName_, portNumber_);
if (tcpSocket_->waitForConnected(1000))
emit connectionEstablished();
exec();
}
This does not :
Code:
#include "connectionthread.h"
#include <QTcpSocket>
ConnectionThread
::ConnectionThread(QObject *parent
){
connected_ = false;
hostName_ = "localhost";
portNumber_ = 4974;
connect(this, SIGNAL(connectionEstablished()), parent, SLOT(connectionEstablished()));
}
ConnectionThread::~ConnectionThread()
{
}
bool ConnectionThread::isConnected() const
{
return connected_;
}
void ConnectionThread::run()
{
tcpSocket_->abort();
tcpSocket_->connectToHost(hostName_, portNumber_);
if (tcpSocket_->waitForConnected(1000))
emit connectionEstablished();
exec();
}
Re: Problem with QTcpSocket in QThread
I think the socket is created in the parent thread's context.
Try calling connectionThread->moveToThread(connectionThread) from wherever you start the connection thread, but after it has started.
Re: Problem with QTcpSocket in QThread
Re: Problem with QTcpSocket in QThread
Quote:
Originally Posted by
jpn
It might be a similar title, but I did not see the link at first. I think more trivial questions have been asked more than one time in the past :)
Re: Problem with QTcpSocket in QThread
If you would have bothered reading the thread, you would know that
- QThread constructor and QThread::run() are executed in different threads
- QThread object itself lives in the thread where it was created
- You shouldn't pass "this" as parent in QThread::run() because "this" lives in another thread
;)
Re: Problem with QTcpSocket in QThread
Kids these days... They don't read anything, just want to give them the code.
Re: Problem with QTcpSocket in QThread
I did not find the link first, so I was not aware of this. I understood the issue after reading the thread, and did not ask any additional things. So what makes you conclude I do not read the info which is given to me?
Just for the record, I am certainly not a kid, and spent a lot of time resolving other issues in Qt without asking questions. I never asked for any code, I just raised a question about an issue I did not understand.
Re: Problem with QTcpSocket in QThread
Looks like it was me who misunderstood your answer. I thought you meant that it was similar but something more trivial that was solved in the linked thread. My apologies for that. No hard feelings.
Re: Problem with QTcpSocket in QThread
Indeed a misunderstanding, I just meant people ask the same question twice in this forum sometimes, although the issue is easier than this problem.
No hard feelings back :).