Share QAxObject between two threads?

旧巷老猫 提交于 2019-12-08 12:58:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!