问题
Let's say an object A running in a thread. It has a pointer to a QAxObject instance and to an object B. The object B has the pointer to the QAxObject.
Object A creates a thread and moves the object B in it.
#ifndef OBJECTA_H
#define OBJECTA_H
#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QAxObject>
#include "ObjectB.h"
class ObjectA : public QObject
{
Q_OBJECT
public:
ObjectA(QObject *parent = 0) : QObject(parent)
{
thread = new QThread();
activeX = new QAxObject();
objectB = new ObjectB(activeX);
objectB->moveToThread(thread);
ObjectA::connect(objectB, SIGNAL(someSignal()), this, SLOT(someSlot()));
thread->start();
}
protected slots:
void someSlot();
private:
QThread *thread;
QAxObject *activeX;
ObjectB *objectB;
};
#endif // OBJECTA_H
#ifndef OBJECTB_H
#define OBJECTB_H
#include <QtCore/QObject>
#include <QAxObject>
class ObjectB : public QObject
{
Q_OBJECT
public:
ObjectB(QAxObject *axObject, QObject *parent = 0) : QObject(parent)
{
activeX = axObject;
}
signals:
void someSignal();
private:
QAxObject *activeX;
};
#endif // OBJECTB_H
Will the object B be able to work with the QAxObject shared with object A?
I feel it won't be possible. Currently, I've got this error: QAxBase: Error calling IDispatch member NewProject: Unknown error
.
And neither the thread A nor the thread B can use the QAxObject.
Any information about this?
回答1:
Here is the explanation: Access a COM object from a 3rd party dll across threads .
Should use:
CoInitialize(0);
CoInitializeEx(NULL, COINIT_MULTITHREADED);
来源:https://stackoverflow.com/questions/15924998/share-qaxobject-between-two-threads