Results 1 to 5 of 5

Thread: QSocketNotifier can not enable from an other thread.

  1. #1
    Join Date
    Mar 2007
    Posts
    5
    Qt products
    Qt4

    Default QSocketNotifier can not enable from an other thread.

    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 :

    Qt Code:
    1. connect(firstClass,SIGNAL(send(int )),secondClass,SLOT(response(int )),Qt::QueuedConnection);
    2.  
    3. firstClass::emitSignal()
    4. {
    5. emit send(1);
    6. }
    7.  
    8. secondClass::run()
    9. {
    10. m_socket = new QTcpSocket();
    11. ....
    12. ...
    13. }
    14. secondClass::response(int a_data)
    15. {
    16. m_socket->write(".....");
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 3rd August 2008 at 18:50. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSocketNotifier can not enable from an other thread.

    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()).

  3. #3
    Join Date
    Mar 2007
    Posts
    5
    Qt products
    Qt4

    Default Re: QSocketNotifier can not enable from an other thread.

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSocketNotifier can not enable from an other thread.

    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 from TT DevDays 2007. Especially the chapter starting from page 32 is important to understand.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSocketNotifier can not enable from an other thread.

    Quote Originally Posted by jpn View Post
    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 from TT DevDays 2007. Especially the chapter starting from page 32 is important to understand.
    cool article!

Similar Threads

  1. Replies: 5
    Last Post: 17th January 2008, 22:49
  2. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 17:48
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  4. Replies: 10
    Last Post: 20th March 2007, 23:19
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 01:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.