Results 1 to 3 of 3

Thread: Exchange information between non-qt pthreads and gui thread

  1. #1
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Exchange information between non-qt pthreads and gui thread

    Hi,

    I am using Qt for having a nice GUI front-end, while the underlying program doesn't use any QThreads and many objects are not created from the main GUI thread.

    There're several PThreads working in the background, which should be able to interact with the GUI. E.g. a module is watching a UDP-Connection and in certain situations it would be nice to signal the GUI to show a dialog to inform the user about it.

    My first try was to use a dummy QObject in the PThread to signal the GUI (by using the signals and slots from the dummy object). But it seems lead to a fatal error:
    <unknown>: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.

    What happens to signals in general? Which thread is looking for the event queue?
    I guess my assumption, that signals are always handled by the main GUI thread, is wrong.

    Btw. is it a problem that my QObject was created from the PThread?

    How to solve such a problem in the right way? (without rewriting all modules and porting them to QThreads etc.)

    Many thanks for your help!

    J.R.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Exchange information between non-qt pthreads and gui thread

    When you mixing frameworks it is a bit more tricky. IMHO safest and the simplest way to call slots from other thread which is not a QThread is to use QMetaObject::invokeMethod with type parameter Qt::QueuedConnection (see Qt::ConnectionType-enum).

    For example try something like this:
    Qt Code:
    1. QMetaObject::invokeMethod(pointerToStateLabel,
    2. "setText",
    3. Qt::QueuedConnection,
    4. Q_ARG(QString, QString("Done"));
    To copy to clipboard, switch view to plain text mode 


    Added after 9 minutes:


    PS. First try to enforce Qt::QueuedConnection when connecting QObject created in this none-qt thread. Just add this value at the end of each connect (default value is Qt::AutoConnection means auto-determinate kind of connection and this may cause your problems).
    Last edited by MarekR22; 8th March 2011 at 11:24.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Exchange information between non-qt pthreads and gui thread

    You can use Qt's event system, it is thread-safe. Just use QCoreApplication::postEvent() and handle the event where you need it. You can additionally use event filters.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Commands exchange between Client and Server
    By QAlex in forum Qt Programming
    Replies: 8
    Last Post: 23rd November 2009, 16:59
  2. Using pthreads for a front end
    By kachofool in forum Newbie
    Replies: 1
    Last Post: 5th October 2009, 22:13
  3. QTableWidget row-column exchange
    By SnarlCat in forum Qt Programming
    Replies: 0
    Last Post: 28th August 2008, 13:49
  4. Tolltech greenphone for exchange or sale
    By izico in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 3rd July 2008, 06:15
  5. CMake and pthreads
    By Matt Smith in forum General Discussion
    Replies: 5
    Last Post: 3rd September 2006, 20:42

Tags for this Thread

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.