在 pyqt4 qobject 类中连接 pyqt4 信号 [英] connecting pyqt4 signals in a pyqt4 qobject class

查看:111
本文介绍了在 pyqt4 qobject 类中连接 pyqt4 信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级;一个用于我的窗口,一个用于我的控制对象

I've got two classes; one for my window and one for my controlling object

class window(baseClass, testForm):
    scanStarted = QtCore.pyqtSignal(str)
    def __init__(self,parent=None):
        super(window, self).__init__(parent)
        self.setupUi(self)

        #other window setup
        self._scanner.pushScan.clicked.connect(self._scanClicked)

    def _scanClicked(self):
        self.scanStarted.emit( self._scanner.getTextData() )

和我的控制对象

class vis(QtCore.QObject):
    def __init__(self):
        self._oreList = []

        self._w = window()
        self._w.scanStarted.connect(self._scanOre)

    def _scanOre(self, rawText):
        print "main ->", rawText

当使用 QtCore.QObject 作为我的参考时,这个信号不会连接到 _scanOre.当我将引用切换到 python 'object' 时,它会正常工作.我一直试图弄清楚为什么它不会使用 QtCore.QObject 类型进行连接.

When using the QtCore.QObject as my reference, this signal won't connect to the _scanOre. When I switch the reference to python 'object' it'll work fine. I've been trying to figure out why it won't connect using the QtCore.QObject type.

无论如何,信号也会在窗口类中正常连接.

The signal will also connect just fine in the window class regardless.

我尝试为 _scanOre 提供 @QtCore.pyqtSlot(str, name='scanGo') 并将 name 参数添加到信号创建中.我不确定我在这里错过了什么.

I tried giving the _scanOre the @QtCore.pyqtSlot(str, name='scanGo') and adding the name parameter into the signal creation as well. I'm not sure what I'm missing here.

推荐答案

你忘记初始化QObject:

class vis(QtCore.QObject):
    def __init__(self, parent=None):
        super(vis, self).__init__(parent) # you are missing this line
                                          # also the `parent` arg
        self._oreList = []

        self._w = window.window()
        self._w.scanStarted.connect(self._scanOre)

    def _scanOre(self, rawText):
        print "main ->", rawText

这篇关于在 pyqt4 qobject 类中连接 pyqt4 信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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