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 :
#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
#ifndef CONNECTIONTHREAD_H
#define CONNECTIONTHREAD_H
#include <QThread>
#include <QString>
class QTcpSocket;
class ConnectionThread : public QThread
{
Q_OBJECT
public:
ConnectionThread(QObject *parent);
~ConnectionThread();
bool isConnected() const;
signals:
void connectionEstablished();
public slots:
//void setHostInfo(QString& hostName, int portNumber);
private:
void run();
QTcpSocket* tcpSocket_;
bool connected_;
QString hostName_;
int portNumber_;
};
#endif // CONNECTIONTHREAD_H
To copy to clipboard, switch view to plain text mode
This works :
#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();
}
#include "connectionthread.h"
#include <QTcpSocket>
ConnectionThread::ConnectionThread(QObject *parent)
: QThread(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_ = new QTcpSocket(this);
tcpSocket_->abort();
tcpSocket_->connectToHost(hostName_, portNumber_);
if (tcpSocket_->waitForConnected(1000))
emit connectionEstablished();
exec();
}
To copy to clipboard, switch view to plain text mode
This does not :
#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();
}
#include "connectionthread.h"
#include <QTcpSocket>
ConnectionThread::ConnectionThread(QObject *parent)
: QThread(parent)
{
connected_ = false;
tcpSocket_ = new QTcpSocket(this);
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks