在GUI线程之外调用QAxWidget方法 [英] Calling QAxWidget method outside of the GUI thread

查看:206
本文介绍了在GUI线程之外调用QAxWidget方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始怀疑这是否不可能,但是我想我想问一下,是否有一种聪明的方法来解决我遇到的问题.

I'm beginning to wonder if this is impossible, but I thought I'd ask in case there's a clever way to get around the problems I'm having.

我有一个使用ActiveX控件的Qt应用程序.该控件由QAxWidget持有,并且QAxWidget本身包含在另一个QWidget中(我需要向该小部件添加其他信号/插槽,并且我不能仅将QAxWidget子类化,因为该类不允许这样做).当我需要与ActiveX控件交互时,我调用QWidget的方法,该方法又调用QAxWidget的dynamicCall方法,以调用ActiveX控件的适当方法.所有这些都工作正常.

I have a Qt application that uses an ActiveX control. The control is held by a QAxWidget, and the QAxWidget itself is contained within another QWidget (I needed to add additional signals/slots to the widget, and I couldn't just subclass QAxWidget because the class doesn't permit that). When I need to interact with the ActiveX control, I call a method of the QWidget, which in turn calls the dynamicCall method of the QAxWidget in order to invoke the appropriate method of the ActiveX control. All of that is working fine.

但是,ActiveX控件的一种方法需要花费几秒钟的时间才能返回.当我调用此方法时,我的整个GUI会锁定几秒钟,直到该方法返回为止.这是不希望的.我希望ActiveX控件自动关闭并自行处理,并在完成时返回我,而无需锁定Qt GUI.

However, one method of the ActiveX control takes several seconds to return. When I call this method, my entire GUI locks up for a few seconds until the method returns. This is undesirable. I'd like the ActiveX control to go off and do its processing by itself and come back to me when it's done without locking up the Qt GUI.

我尝试了一些没有成功的事情:

I've tried a few things without success:

  • 创建一个新的QThread并从新线程中调用QAxWidget :: dynamicCall
  • 将信号连接到QAxWidget的相应插槽方法,并使用信号/插槽而不是dynamicCall调用该方法
  • 使用QtConcurrent :: run调用QAxWidget :: dynamicCall
  • Creating a new QThread and calling QAxWidget::dynamicCall from the new thread
  • Connecting a signal to the appropriate slot method of the QAxWidget and calling the method using signals/slots instead of using dynamicCall
  • Calling QAxWidget::dynamicCall using QtConcurrent::run

似乎没有任何影响行为.无论我在何处或何处使用dynamicCall(或触发QAxWidget的相应插槽),GUI都将锁定,直到ActiveX控件完成其操作为止.

Nothing seems to affect the behavior. No matter how or where I use dynamicCall (or trigger the appropriate slot of the QAxWidget), the GUI locks until the ActiveX control completes its operation.

是否有任何方法可以将此ActiveX处理与Qt GUI线程分离,以使在ActiveX控件运行方法时GUI不会锁定?我可以用QAxBase或QAxObject做些聪明的事情来获得想要的结果吗?

Is there any way to detach this ActiveX processing from the Qt GUI thread so that the GUI doesn't lock up while the ActiveX control is running a method? Is there something clever I can do with QAxBase or QAxObject to get my desired results?

推荐答案

经过一些实验,我能够通过做一些我认为较早尝试过的事情来解决此问题:创建一个新的QThread并从该窗口中调用QAxWidget :: dynamicCall新线程.第一次尝试此解决方案时,我一定不能正确编码.与同事坐在一起后,我们得以使它运转.具体来说,我们所做的是:

After some experimentation, I was able to solve this by doing something I thought I'd tried earlier: creating a new QThread and calling QAxWidget::dynamicCall from the new thread. I must not have coded it correctly the first time I tried this solution; after sitting with a co-worker, we were able to get it to work. To be specific, what we did is:

(第1步)创建了QThread的子类来表示我需要调用dynamicCall()的线程.

(Step 1) Created a subclass of QThread to represent the thread I need to call dynamicCall().

(第2步)在我的QThread的构造函数中,传递一个指向原始QAxWidget的指针,并将该指针保留在成员变量中.

(Step 2) In the constructor of my QThread, pass in a pointer to my original QAxWidget, and keep the pointer in a member variable.

MyThread::MyThread(QAxWidget* passedWidget) : QThread()  
{  
    actualWidget = passedWidget;  
}  

(步骤3)在QThread的run()方法中,调用QAxWidget的dynamicCall()方法.

(Step 3) In the run() method of the QThread, call the dynamicCall() method of the QAxWidget.

void MyThread::run()  
{  
    QVariant result = actualWidget->dynamicCall(...parms as necessary...);  
}  

(第4步)回到我的主代码中,当我需要执行dynamicCall()时,只需调用MyThread的start()方法. start()方法将在其自己的线程中执行run(),从而向ActiveX对象发送必要的命令,而不会阻塞或停止主GUI线程.

(Step 4) Back in my main code, when I need to execute dynamicCall(), I just call the start() method of MyThread. The start() method will execute run() in its own thread, thus sending the necessary command to the ActiveX object without blocking or stalling the main GUI thread.

这篇关于在GUI线程之外调用QAxWidget方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆