Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to move one QAxObject from one thread to another
Forum Updated to NodeBB v4.3 + New Features

How to move one QAxObject from one thread to another

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alexKI
    wrote on last edited by
    #1

    I have a TestConnector class that creates com-object in one thread and moves it to another thread after initialization (asynchronous initializator)):

    testconnector.h
    @#ifndef TESTCONNECTOR_H
    #define TESTCONNECTOR_H

    #include <ActiveQt/qaxobject.h>
    #include <ActiveQt/qaxbase.h>
    #include <QThreadPool>
    #include <QObject>

    class TestConnector : public QObject, public QRunnable
    {
    Q_OBJECT

    public:
    TestConnector(QThread *recieveThread);
    void run();
    void setRecieveThread(QThread *t);

    signals:
    void testConnectionFailed();
    void testConnected(QAxObject *ax);

    protected:
    QThread *recieveThread;
    };

    #endif // TESTCONNECTOR_H@

    testconnector.cpp
    @#include "testconnector.h"
    #include <QDebug>
    #include <shlobj.h>

    TestConnector::TestConnector(QThread *recieveThread) :
    QObject(0)
    {
    setRecieveThread(recieveThread);
    }

    void TestConnector::setRecieveThread(QThread *t)
    {
    this->recieveThread = t;
    }

    void TestConnector::run()
    {
    qDebug("TestConnector: Initializing COM library");
    HRESULT h_result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    switch(h_result)
    {
    case S_OK:
    qDebug("TestConnector: The COM library was initialized successfully on this thread");
    break;
    case S_FALSE:
    qWarning("TestConnector: The COM library is already initialized on this thread");
    break;
    case RPC_E_CHANGED_MODE:
    qWarning() << "TestConnector: A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA)."
    << " This could also indicate that a change from neutral-threaded apartment to single-threaded apartment has occurred";
    break;
    }

    qDebug("TestConnector: Connecting to Ax");
    QAxObject *ax = new QAxObject("Word.Application", 0);
    
    if(ax->isNull())
    {
        qCritical("TestConnector: Ax connection failed");
        delete ax;
        ax = 0;
        emit testConnectionFailed();
    }
    else
    {
        qDebug("TestConnector: Ax connected successfully");
        ax->moveToThread(recieveThread);
        emit testConnected(ax);
    }
    

    }@

    after this manipulations I have QAxObject in recieveThread thread, but com-instance how I understand still in thread of TestConnector class. And the subject of question is how to move QAxObject with it child com-instance into another thread?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      That will beb tricks
      moving COM pointers between threads normally means calling CoMarshalInthredaInterFaceInStream and also an unmarshal. How it works with QAxObject, I don't know. I suggest look at the code of QAxObject and check, whether they do some special things in moveToThread

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexKI
        wrote on last edited by
        #3

        I'm watching now the qaxobject.h, and can't find reimplemented moveToThread:

        @class QAxObject : public QObject, public QAxBase
        {
        friend class QAxEventSink;
        public:
        const QMetaObject metaObject() const;
        void
        qt_metacast(const char*);
        int qt_metacall(QMetaObject::Call, int, void );
        QObject
        qObject() const { return (QObject
        )this; }
        const char *className() const;

        QAxObject(QObject *parent = 0);
        QAxObject(const QString &c, QObject *parent = 0);
        QAxObject(IUnknown *iface, QObject *parent = 0);
        ~QAxObject();
        
        bool doVerb(const QString &verb);
        

        protected:
        void connectNotify(const char *signal);

        private:
        const QMetaObject *parentMetaObject() const;
        static QMetaObject staticMetaObject;
        };@

        I think it is the theme for bugreport.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          Perhaps QAxObject should not be movable...

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alexKI
            wrote on last edited by
            #5

            but it is implemented and that confuse

            PS http://bugreports.qt.nokia.com/browse/QTBUG-18521

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Maxbester
              wrote on last edited by
              #6

              Have you tried passing a reference of the QAxObject to the other thread?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved