PDA

View Full Version : QAxObject worker thread



semajnosnibor
20th January 2012, 21:11
I'm having an odd issue with a worker thread I've created.
I have the line 'CoInitialize(0);' in the threads run(). The worker thread basically communicates with EXCEL, so it has a bunch of public member functions to accomplish that. However any of the activeQT calls within those public member functions only operate correctly when called from within the worker thread (specifically in the run() function).
When the worker thread is in the event loop, none of the ActiveQT calls work (they act as if CoInitialize() has not been called).

Basically my steps are:
1) create the worker thread
2) call the 'start()' on the worker thread (which calls run())
3) run initializes the QAxObject
4) once the QAxObjet is created, run calls exec();

After exec() was called, I was under the impression I could use the workerThreadObject like any other object...

KillGabio
20th January 2012, 22:42
http://www.youtube.com/watch?v=JaGqGhRW5Ks&list=PL2D1942A4688E9D63&index=28&feature=plpp_video

let me know if these videos help ;) im for the 22 but i ve learnt a lot

blueSpirit
21st January 2012, 17:10
I haven't watched the youtube tutorial - so maybe the solution is already there, but this is who I understand the Qt thread 'framework' - and how some guys from Qt also explain it:


int main( ... )
{
QApplication oApp( ... );
QThread oMyThread;
MyQObject oMyObject;
oMyObject.moveToThread( &oMyThread );

// Important to use AutoConnection or QueuedConnection here!
QMetaObject::invokeMethod( &oMyObject, "SL_DoSomething", Qt::QueuedConnection );

oApp.exec();
}

The point is, that you don't derive from QThread (because so you can attach 10 objects to one thread) but move the object to the thread. Notice also, that only the work in the slots is performed in this thread (and not e.g. the constructor or other direct calls!). So you could place your methods (which were previously in the run() method) into the SL_DoSomething() slot and invoke the method by the signal-slot-mechanism.

When you call SL_DoSomething() directly (without emitting a signal or using QMetaObject::invokeMethod()) you are still in the main thread.