[Qt-interest] Can't post a user event from compute thread to GUI thread
Moinier, Pierre (UK)
Pierre.Moinier at baesystems.com
Tue Nov 10 12:14:03 CET 2009
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.
********************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091110/3a784c08/attachment.html
More information about the Qt-interest-old
mailing list