PDA

View Full Version : Thread(s) and socket, timer slots



stephdev1965
8th November 2006, 12:25
Hi all

I wrote routines in a form using a QTCpSocket and a QTimer
to receive a non stop data stream from a server and write data from buffer in a different file each minute. It worked fine in the dialog form.

So i tried to write a thread class involving a QSocket and a QTimer too.
I would like to run multiple threads without freezing the user interface.
My thread runs well but events are never fired... :eek:
The code compiles well, but returns the following messages on execution :


QObject: Cannot create children for a parent that is in a different thread.
Object::connect: No such slot ThreadReception::readStream()
Object::connect: No such slot ThreadReception::displayError(QAbstractSocket::Soc ketError)
QObject: Cannot create children for a parent that is in a different thread.
Object::connect: No such slot ThreadReception::topMinute()

ecc.



class ThreadReception : public QThread
{
Q_OBJECT

public:

ThreadReception(QObject *parent = 0);
~ThreadReception();

bool connectSource();
void unconnectSource();

QMutex threadMutex;

QTcpSocket *tcpSocket;
QTimer *timerHorloge;

(...)

private:
void readStream();
void topMinute();
void saveMinuteBuffer();

protected:
void run();
void attente();

};


void ThreadReception::run()
{
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readStream()), Qt::DirectConnection);
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)), Qt::DirectConnection);

//initialise un timer
timerClock = new QTimer(this);
connect(timerClock, SIGNAL(timeout()), this, SLOT(topSeconde()), Qt::DirectConnection);
timerHorloge->start(1000); //1 seconde

exec();

}

i modified my code as below. first messages relative to the "parent thread" have disappeared but now i get the following messages


QObject::connect: Cannot connect QTcpSocket::readyRead() to (null)::readStream()
QObject::connect: Cannot connect QTcpSocket::error(QAbstractSocket::SocketError) to (null)::displayError(QAbstractSocket::SocketError)
QObject::connect: Cannot connect QTimer::timeout() to (null)::topMinute()


void ThreadReception::run()
{
tcpSocket = new QTcpSocket(0);
connect(tcpSocket, SIGNAL(readyRead()), 0, SLOT(readStream()), Qt::DirectConnection);
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), 0, SLOT(displayError(QAbstractSocket::SocketError)), Qt::DirectConnection);

//initialise un timer
timerClock = new QTimer(0);
connect(timerClock, SIGNAL(timeout()), 0, SLOT(topSeconde()), Qt::DirectConnection);
timerHorloge->start(1000); //1 seconde

exec();

}

What is wrong ? Has someone any idea ?:confused:

Best regards

jpn
8th November 2006, 14:04
What is wrong ? Has someone any idea ?
The problem is exactly what the error states. "Cannot connect something to null". You are passing "0" as receiver in the connect statements. Here's a thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-problems-with-qthread-and-qtcpsocket-4242.html) worth reading.