connecting pyqt4 signals in a pyqt4 qobject class

爷,独闯天下 提交于 2019-12-12 00:21:21

问题


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() )

and my controlling object

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

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.

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.


回答1:


You forgot to initialize the 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


来源:https://stackoverflow.com/questions/12694474/connecting-pyqt4-signals-in-a-pyqt4-qobject-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!