【问题标题】:How to receive ActiveX events in pyQt5?如何在 pyQt5 中接收 ActiveX 事件?
【发布时间】:2017-03-09 16:24:13
【问题描述】:

我一直在使用 pyQt4。我想转换 pyQt5. 但是,我不能在 pyQt5 中使用 old-style signal and slot,因为 pyQt5 仅支持 new-style signal and slot。 因此,我无法从 ActiveX 接收事件。

请给我解决方案。

此代码为pyQt4版本。

from PyQt4.QtCore import SIGNAL, QObject
from PyQt4.QAxContainer import QAxWidget

class ActiveXExtend(QObject):
    def __init__(self, view):
        super().__init__()
        self.view = view
        self.ocx = QAxWidget("KHOPENAPI.KHOpenAPICtrl.1")

        # receive ActiveX event.
        self.ocx.connect(self.ocx, SIGNAL("OnReceiveMsg(QString, QString, QString, QString)"), self._OnReceiveMsg)

    # event handler
    def _OnReceiveMsg(self, scrNo, rQName, trCode, msg):
        print("receive event")

我尝试转换 pyQt5。

from PyQt5.QtCore import QObject
from PyQt5.QAxContainer import QAxWidget

class ActiveXExtend(QObject):

    def __init__(self, view):
        super().__init__()
        self.view = view
        self.ocx = QAxWidget("KHOPENAPI.KHOpenAPICtrl.1")
        # receive ActiveX event. 
        # old-style is not supported.
        # self.ocx.connect(self.ocx, SIGNAL("OnReceiveMsg(QString, QString, QString, QString)"), self._OnReceiveMsg)

    # event handler
        def _OnReceiveMsg(self, scrNo, rQName, trCode, msg):
            print("receive event") 

【问题讨论】:

标签: python activex pyqt5


【解决方案1】:

我终于找到了解决方案。 pyQt5 支持来自 ActiveX 事件的信号。

如果 ActiveX 有 'OnReceiveMsg' 事件,QAxWidget 实例支持 'OnReceiveMsg' 信号。 因此,我修复了这样的代码。

from PyQt5.QtCore import QObject
from PyQt5.QAxContainer import QAxWidget

class ActiveXExtend(QObject):

    def __init__(self, view):
        super().__init__()
        self.view = view
        self.ocx = QAxWidget("KHOPENAPI.KHOpenAPICtrl.1")
        # receive ActiveX event. 
        self.ocx.OnReceiveMsg[str,str,str,str].connect(self._OnReceiveMsg)

    # event handler
        def _OnReceiveMsg(self, scrNo, rQName, trCode, msg):
            print("receive event") 

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 1970-01-01
    • 2015-07-30
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    相关资源
    最近更新 更多