[Qt-interest] Can't post a user event from compute thread to GUI thread

S. Aguinaga sa_jr at yahoo.com
Wed Feb 24 13:52:06 CET 2010


Hello Pierre,
Would you mind posting what your solution was?
I hoping this could help me sort out an issue I have --where I'm trying to post an event from outside the GUI (or main) thread, but after reading this, it might make more sense to use a new thread that looks for the setting of a global flag and then update my GUI widget.

// Salvador



________________________________
From: "Moinier, Pierre (UK)" <Pierre.Moinier at baesystems.com>
To: qt-interest at trolltech.com
Sent: Mon, November 16, 2009 3:19:09 AM
Subject: Re: [Qt-interest] Can't post a user event from compute thread to GUI thread

Thanks to all of you who have contributed!

I also found out what my problem was, so for the record, here is the reason: the constructor UpdateView was only run when UpdateView::instance was called for the first time from the compute thread with the object belonging to the compute thread. The compute thread did not run an event loop so my events were never delivered.

I also manged to use signal and slot and it does work like a charm

   -Pierre.


-----Original Message-----
From: Louai Al-Khanji [mailto:louai.khanji at gmail.com] 
Sent: 10 November 2009 23:03
To: Moinier, Pierre (UK)
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Can't post a user event from compute thread to GUI thread

                    *** WARNING ***

  This message has originated outside your organisation,
  either from an external partner or the Global Internet. 
      Keep this in mind if you answer this message.


Hi again,

Here's a slightly cleaner version.

-- Louai

On Tue, Nov 10, 2009 at 5:43 PM, Louai Al-Khanji <louai.khanji at gmail.com> wrote:
> Hi
>
> The attached works for me.
>
> -- Louai
>
> On Tue, Nov 10, 2009 at 8:48 AM, Moinier, Pierre (UK) 
> <Pierre.Moinier at baesystems.com> wrote:
>> Hi!
>>
>> I have done what you suggested, but the behaviour is the same. 
>> Nothing seems to go to event()...
>>
>> So I commented out the call to installEventFilter and to evenFilter 
>> and here is my reimplementation of event():
>>
>>  bool UpdateView::event(QEvent *event)
>>  {
>>        std::cout << "hello" << std::endl;
>>      if (event->type() == QEvent::User) {
>>        std::cout << "hello ... found the event!" << std::endl;
>>              return true;
>>          }
>>
>>      else
>>        return false;
>>  }
>> If it is easier, I could tar up the couple of files and send it to you...
>>
>> Regards,
>>
>>     -Pierre.
>> ________________________________
>> From: Mihail Naydenov [mailto:mlists at ymail.com]
>> Sent: 10 November 2009 12:35
>> To: Moinier, Pierre (UK); qt-interest at trolltech.com
>> Subject: Re: [Qt-interest] Can't post a user event from compute 
>> thread to GUI thread
>>
>>                     *** WARNING ***
>>
>>   This message has originated outside your organisation,
>>   either from an external partner or the Global Internet.
>>       Keep this in mind if you answer this message.
>>
>> After just a quick look, why do you use an event filter, when you are 
>> sending events directly to that object?
>> Just reimplement event() in UpdateView and you will be ok.
>>
>> Also note that your implementation of instance() is NOT thread safe. 
>> You can Q_GLOBAL_STATIC macro to help you out in that.
>> (no how to use it, look at QThreadPool or some other singleton in 
>> qt's src)
>>
>> MihailNaydenov
>>
>> From: "Moinier, Pierre (UK)" <Pierre.Moinier at baesystems.com>
>> To: qt-interest at trolltech.com
>> Sent: Tue, November 10, 2009 1:14:03 PM
>> Subject: [Qt-interest] Can't post a user event from compute thread to 
>> GUI thread
>>
>> Hi!
>>
>> I am trying to figure out how to send an event fom a thread to the 
>> main visualisation thread using postEvent, installEventFilter and eventFilter.
>> I am pasting parts of the test code I am using, which compiles, but 
>> somehow, the event is probably not reaching the filter, beause I am 
>> not geting any messages from eventFilter.
>>
>> Could anyone tell me what I am doing wrong?
>>
>> Regards,
>>
>>     -Pierre.
>>
>> -------------------------------------------------------------  Test 
>> Code
>> ------------------------------------------------------------------
>> main.cpp:
>>
>> #include <QApplication>
>>
>> #include "ComputeThread.h"
>> #include <qpushbutton.h>
>>
>> int main(int argc, char* argv[])
>> {
>>   QApplication app(argc, argv);
>>   ComputeThread thread;
>>   thread.start();
>>
>>   QPushButton hello("Hello world!");
>>   hello.resize(100, 30);
>>
>>   hello.show();
>>   return app.exec();
>> }
>> ComputeThread.cpp:
>>
>> #include <iostream>
>> #include <QApplication>
>> #include <QEvent>
>>
>> #include "ComputeThread.h"
>> #include "UpdateView.h"
>>
>> void ComputeThread::run()
>> {
>>   for (int i=0;i<100;i++)
>>     {
>>       sleep(1);
>>       std::cout << "sending Event...." << UpdateView::UPDATE_EV1 << 
>> std::endl;
>>       QEvent *e = new QEvent(QEvent::User);
>>       QApplication::postEvent(&UpdateView::instance(), e);
>>       //  QCoreApplication::postEvent(&UpdateView::instance(), new 
>> QEvent(static_cast<QEvent::Type>(UpdateView::UPDATE_EV1)));
>>     }
>> }
>> UpdateView.cpp:
>>
>> #include <iostream>
>> #include <QEvent>
>>
>> #include "UpdateView.h"
>>
>> UpdateView::UpdateView()
>> {
>>   std::cout << "hello sailor" << std::endl;
>>   installEventFilter(this);
>> }
>>
>> bool UpdateView::eventFilter(QObject *obj, QEvent *event) {
>>   std::cout << "hello" << std::endl;                                  
>> !!! I should get Hello on the sceen.....
>>   const Event ev = static_cast<Event>(event->type());
>>   switch(ev)
>>     {
>>     case UPDATE_EV1:
>>       std::cout << "Updating the scene..." << std::endl;
>>       break;
>>
>>     default:
>>       return false;
>>     }
>>   return true;
>> }
>> UpdateView.h:
>>
>> #ifndef UPDATEVIEW_H
>> #define UPDATEVIEW_H
>>
>> #include <QEvent>
>> #include <QObject>
>>
>> class UpdateView : public QObject
>> {
>>   Q_OBJECT
>>     public:
>>   UpdateView();
>>   static UpdateView& instance()
>>     {
>>       static UpdateView theinstance;
>>       return theinstance;
>>     }
>>
>>   enum Event {UPDATE_EV1 = 1000 , UPDATE_EV2 = 1001};
>>
>>   bool eventFilter(QObject *obj, QEvent *event);
>>
>> };
>>
>> #endif
>>
>> ********************************************************************
>> This email and any attachments are confidential to the intended 
>> recipient and may also be privileged. If you are not the intended 
>> recipient please delete it from your system and notify the sender.
>> You should not copy it or use it for any purpose nor disclose or 
>> distribute its contents to any other person.
>> ********************************************************************
>>
>>
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>
>
>
> --
> - Louai Al-Khanji
>



--
- Louai Al-Khanji

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100224/5173b771/attachment.html 


More information about the Qt-interest-old mailing list