Communicating with a QThread - KDE

This is a discussion on Communicating with a QThread - KDE ; Hello! I have a QT application which has a main class (handling the GUI) and a thread class (it inherits from QThread) called Worker. Now Worker would like to change the texts in QLabels. I thought that signals and slots ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Communicating with a QThread

  1. Communicating with a QThread

    Hello!

    I have a QT application which has a main class (handling the GUI) and a thread
    class (it inherits from QThread) called Worker. Now Worker would like to
    change the texts in QLabels. I thought that signals and slots (with emit and
    connect) would help here, but then Worker would have to also inherit from
    QObject? Is there some other way to communicate between the 2?

    Thierry

  2. Re: Communicating with a QThread

    Thierry Dussuet wrote:
    > I have a QT application which has a main class (handling the GUI) and a
    > thread class (it inherits from QThread) called Worker. Now Worker would
    > like to change the texts in QLabels. I thought that signals and slots
    > (with emit and connect) would help here, but then Worker would have to
    > also inherit from QObject? Is there some other way to communicate between
    > the 2?


    In Qt3, thread support is very limited. Read the documentation on threads,
    it's quite good. From http://doc.trolltech.com/3.3/threads.html#8 :

    "Any operations that generate events must not be called by any thread other
    than the GUI thread."

    If you need to post events to the GUI Thread, use QApplication:ostEvent()
    from the worker thread. This means that in Qt3, communication between
    threads is best done via events, since signals and slots cannot be used
    across thread boundaries in Qt3. Qt4 makes that possible, since calling a
    slot is now asynchronous.

    Cheers,
    Daniel

  3. Re: Communicating with a QThread

    On 2005-05-11, Daniel Molkentin wrote:
    > Thierry Dussuet wrote:
    >> I have a QT application which has a main class (handling the GUI) and a
    >> thread class (it inherits from QThread) called Worker. Now Worker would
    >> like to change the texts in QLabels. I thought that signals and slots
    >> (with emit and connect) would help here, but then Worker would have to
    >> also inherit from QObject? Is there some other way to communicate between
    >> the 2?

    >
    > In Qt3, thread support is very limited. Read the documentation on threads,
    > it's quite good. From http://doc.trolltech.com/3.3/threads.html#8 :
    >
    > "Any operations that generate events must not be called by any thread other
    > than the GUI thread."
    >
    > If you need to post events to the GUI Thread, use QApplication:ostEvent()
    > from the worker thread. This means that in Qt3, communication between
    > threads is best done via events, since signals and slots cannot be used
    > across thread boundaries in Qt3. Qt4 makes that possible, since calling a
    > slot is now asynchronous.


    Thank you for your answer! I looked at the events and they look promising, but
    I still have a small problem. The installEventFilter call is ok, but then,
    when I want to post it from the thread to the main application, how should I
    dod that? postEvent needs an object to post the event to, but I can't have one
    of the main application in the thread, can I?

    Thierry

  4. Re: Communicating with a QThread

    Thierry Dussuet wrote:
    > Thank you for your answer! I looked at the events and they look
    > promising, but
    > I still have a small problem. The installEventFilter call is ok, but
    > then, when I want to post it from the thread to the main application, how
    > should I
    > dod that? postEvent needs an object to post the event to, but I can't
    > have one of the main application in the thread, can I?


    You don't need to. the object you post to can be in the gui thread, posting
    events works across thread boundaries, that's the trick

    Cheers,
    Daniel

  5. Re: Communicating with a QThread

    On 2005-05-11, Daniel Molkentin wrote:
    > Thierry Dussuet wrote:
    >> Thank you for your answer! I looked at the events and they look
    >> promising, but
    >> I still have a small problem. The installEventFilter call is ok, but then,
    >> when I want to post it from the thread to the main application, how should I
    >> do that? postEvent needs an object to post the event to, but I can't have
    >> one of the main application in the thread, can I?

    >
    > You don't need to. the object you post to can be in the gui thread, posting
    > events works across thread boundaries, that's the trick


    Thank you! It works now! :-)

    But maybe a question more -- When sending several events, is it really safe to
    just create a new one each time? Or should I destroy them in the eventFilter?

    Best regards,

    Thierry

  6. Re: Communicating with a QThread

    Thierry Dussuet wrote:

    > But maybe a question more -- When sending several events, is it really
    > safe to
    > just create a new one each time? Or should I destroy them in the
    > eventFilter?


    Dont, the event loop takes ownership.

    Best regards,

    Daniel


  7. Re: Communicating with a QThread

    Hello,

    We were also facing the same issue. Could you please post the piece of code to post the event?

    Thanks.

+ Reply to Thread