[Qt-interest] Can't post a user event from compute thread to GUI thread
Louai Al-Khanji
louai.khanji at gmail.com
Tue Nov 10 23:43:27 CET 2009
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 1810 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091110/f3624953/attachment.bin
More information about the Qt-interest-old
mailing list