PDA

View Full Version : QSocketNotifier can not enable from an other thread.



dabiabilus
3rd August 2008, 17:46
I have a two classes derived from QThread

First class emits a signal to the second class when an event occurs. I connected these signal and slot with queud connection explicitly. Second class try to write to a tcp socket created in the run block of the second class. but it gives the error "QSocketNotifier can not enable from an other thread." I coul not understand why this happen.

Code is beriefly below :


connect(firstClass,SIGNAL(send(int )),secondClass,SLOT(response(int )),Qt::QueuedConnection);

firstClass::emitSignal()
{
emit send(1);
}

secondClass::run()
{
m_socket = new QTcpSocket();
....
...
}
secondClass::response(int a_data)
{
m_socket->write(".....");
}

jacek
5th August 2008, 00:54
The problem is that QThread object lives in the thread that created it, not the thread it represents. So if you connect something through a queued connection to QThread object, signals will be queued in wrong place.

You have to either split your implementation into a thread and a task that lives in it or move that thread to itself (see QObject::moveToThread()).

dabiabilus
5th August 2008, 08:23
I did not understanda at all.

Both thread class lives in main thread. but when I connect both thread with direct connection than, as I know , slot will invoked in the caller thread event loop. But , it does not have permission to use socket object , created in the second thread.

jpn
5th August 2008, 12:30
Try to print QThread::currentThread() in the slot and you'll see the problem. I'd recommend reading the Multithreading presentation by Bradley T. Hughes (http://chaos.troll.no/~ahanssen/devdays2007/DevDays2007-Threading.pdf) from TT DevDays 2007. Especially the chapter starting from page 32 is important to understand.

spirit
5th August 2008, 13:31
Try to print QThread::currentThread() in the slot and you'll see the problem. I'd recommend reading the Multithreading presentation by Bradley T. Hughes (http://chaos.troll.no/~ahanssen/devdays2007/DevDays2007-Threading.pdf) from TT DevDays 2007. Especially the chapter starting from page 32 is important to understand.

cool article! :)