我正在从另一个类发送一个信号来更新PySide QTableWidget但是什么都没有发生。我对这个演示非常简单:
这是在名为Records.py
class Records(QDialog, randomDialog.Ui_watchingDialog):
signal = 1
atSig = Signal(int)
def add_button_clicked(self):
# Do some stuff
self.signal = 1
self.atSig.emit(self.signal)
# Do some other Stuff
这位于名为main.py
from controller import Records
class main(QMainWindow, pyMainWindow.Ui_mainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
signal_records = Records.Records()
signal_records.atSig.connect(self.showNewData)
def showNewData(self, signal):
if signal == 1:
print "It worked!"
else:
print "Problem"
为什么这个信号没有通过?没有抛出任何错误消息,也没有调用任何打印语句。我该如何解决这个问题?
答案 0 :(得分:3)
signal_records
返回并且被垃圾收集, main.__init__()
就会超出范围。您需要使其成为main
的成员,以便它在课程的生命周期中持续存在。
self.signal_records = Records.Records()
self.signal_records.atSig.connect(self.showNewData)
或者,您可以指定main
作为signal_records
signal_records = Records.Records(self)
这两种方法都可以确保引用signal_records
。
答案 1 :(得分:0)
如果可能,请在来电之前定义;
.
.
@Slot (int)
def showNewData(self, signal):
.
.
或者不是,请检查您的来电者def add_button_clicked(self)
。我在pyqt4(相同的pySide)和(我剪掉了一些部分,并在你的代码中放了一些测试路径)代码工作。这是工作。
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class QRecordsDialog (QtGui.QDialog):
addButtonClickedSignal = QtCore.pyqtSignal(int)
def __init__ (self, parent = None):
super(QRecordsDialog, self).__init__(parent)
self.myQPushButton = QtGui.QPushButton('Test Signal', self)
self.myQHBoxLayout = QtGui.QHBoxLayout()
self.myQHBoxLayout.addWidget(self.myQPushButton)
self.setLayout(self.myQHBoxLayout)
self.myQPushButton.clicked.connect(self.addButtonClicked)
def addButtonClicked (self):
self.addButtonClickedSignal.emit(1)
class QMainWindow (QtGui.QMainWindow):
def __init__ (self, parent = None):
super(QMainWindow, self).__init__(parent)
myQRecordsDialog = QRecordsDialog(self)
myQRecordsDialog.addButtonClickedSignal.connect(self.showNewData)
myQRecordsDialog.show()
@QtCore.pyqtSlot(int)
def showNewData (self, signal):
if signal == 1:
print "It worked !"
else:
print "Problem ?"
myQApplication = QtGui.QApplication(sys.argv)
myQMainWindow = QMainWindow()
myQMainWindow.show()
sys.exit(myQApplication.exec_())
如果要修改PyQt代码以使用PySide命名方案,可以使用简单的定义来完成:
QtCore.Signal = QtCore.pyqtSignal
QtCore.Slot = QtCore.pyqtSlot
此致