[Qt-interest] cannot send posted events for objects in another thread
Yifei Li
yifli at mtu.edu
Tue Apr 14 23:26:13 CEST 2009
Hi all,
I know there're already lots of threads on this issue, but none of them
answer my question. Here's a self-contained small example that shows
how I got the above error message:
I have a global object of Foo and all the work is done in Foo:doWork,
Foo has a signal telling us its progress. Then in WorkerThread::run(),
Foo::doWork is called. I connet Foo's signal to a slot in WorkerThread,
and in the slot I updated the value of a progress dialog.
WorkerThread::run() is called somewhere else in the application.
However, I get the following errors when trying to run the program:
QCoreApplication::sendPostedEvents: Cannot send posted events for
objects in another thread
( the above message is repeated here)
MyApp: xcb_lock.c:77: _XGetXCBBuffer: Assertion `((int) ((xcb_req) -
(dpy->request)) >= 0)' failed.
I'm pretty sure it's that global object that causes the problem. But
my questions are:
1) why this happens?
2) what I need to do to make the program working without getting rid of
the global object ( in real apps, that global object is defined in a
3rd party libarary)
Any help is highly appreciated!
Yifei
=======Foo.h==========
class Foo : public QObject
{
Q_OBJECT
signals:
void currentStep(int)
public:
void doWork();
}
extern Foo foo; // my global object
======Foo.cpp======
Foo foo;
void Foo::doWork()
{
// do something
emit currentStep(i); // tell us its progress
}
=====WorkerThread.h======
class WorkerThread : public QThread
{
public:
WorkerThread();
private slots:
void currentStep(int);
protected:
void run();
private:
QProgressDialog * pd;
}
======WorkerThread.cpp=====
WorkerThread::WorkerThread()
{
pd = new QProgressDialog(...);
connect( &foo, SIGNAL(currentStep(int)), this, SLOT(currentStep(int)) );
}
void WorkerThread::currentStep(int i)
{
pd->setValue(i); // update progress dialog
}
void WorkerThread::run()
{
pd->show();
foo.doWork(); // do the work
}
More information about the Qt-interest-old
mailing list