本文介绍了在两个线程之间共享QAxObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个对象A在线程中运行。它具有指向QAxObject实例和对象B的指针。对象B具有指向QAxObject的指针。

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.

对象A创建一个线程并将对象B移动

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

对象B是否能够工作与对象A共享的QAxObject?

Will the object B be able to work with the QAxObject shared with object A?

我觉得这是不可能的。目前,我有这个错误: QAxBase:调用IDispatch成员时出错:NewProject:未知错误

I feel it won't be possible. Currently, I've got this error: QAxBase: Error calling IDispatch member NewProject: Unknown error.

线程A和线程B都可以使用QAxObject。

And neither the thread A nor the thread B can use the QAxObject.

有关这方面的任何信息吗?

Any information about this?

推荐答案

以下是解释:通过线程从第三方dll访问COM对象。

Here is the explanation: Access a COM object from a 3rd party dll across threads .

应使用:

CoInitialize(0);
CoInitializeEx(NULL, COINIT_MULTITHREADED);

这篇关于在两个线程之间共享QAxObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!