PySide 发出信号导致 python 崩溃 [英] PySide emit signal causes python to crash
问题描述
我正在阅读使用 Python 和 Qt 进行快速 GUI 编程"这本书,并且在信号/插槽项目上遇到了问题.我已经下载了作者的代码来与我自己的代码进行比较,看起来都一样,但是,当我从派生的旋转框类发出信号时,python 只是崩溃了.这是我拥有的完整代码:
I am working through the book "Rapid Gui Programming with Python and Qt" and am having a problem on the signals/slots project. I have downloaded the authors code to compare against my own, and it all looks the same, however, when I emit a signal from a derived spin box class, python just crashes. Here is the entire code that I have:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class ZeroSpinBox(QSpinBox):
zeros = 0
def __init__(self, parent=None):
super(ZeroSpinBox, self).__init__(parent)
self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero)
def checkzero(self):
if self.value() == 0:
self.zeros += 1
self.emit(SIGNAL("atzero"), self.zeros)
class Form(QDialog):
def __init__(self, parent= None):
super(Form, self).__init__(parent)
dial = QDial()
dial.setNotchesVisible(True)
spinbox = ZeroSpinBox()
spinbox.setRange(0,200)
dial.setRange(0,200)
layout = QHBoxLayout()
layout.addWidget(dial)
layout.addWidget(spinbox)
self.setLayout(layout)
self.connect(dial, SIGNAL("valueChanged(int)"), spinbox, SLOT("setValue(int)"))
self.connect(spinbox, SIGNAL("valueChanged(int)"), dial, SLOT("setValue(int)"))
self.connect(spinbox, SIGNAL("atzero"), self.announce)
self.setWindowTitle("Signals and Slots Part 2")
def announce(self, zeros):
print "ZeroSpinBox has been at zero %d times" % zeros
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
我的问题发生在 spinbox 归零时,调用 checkzero(self) 方法(ZeroSpinBox 类的),self.zeros += 1 行是好的,然后在发射行上 windows 报告 Python.exe崩溃了.我得到的错误是python.exe 已停止工作",控制台报告进程已完成,退出代码为 -1073741819"
My problem occurs when the spinbox goes down to zero, the checkzero(self) method (of the ZeroSpinBox class) gets called, the self.zeros += 1 line is ok, then on the emit line windows reports that Python.exe has crashed. The error I get is "python.exe has stopped working" and the console reports "Process finished with exit code -1073741819"
知道为什么会这样吗?这是 Python 2.7.2 和 PyQT4 w/PySide.
Any idea why this is happening? This is Python 2.7.2 and PyQT4 w/PySide.
推荐答案
Substitute SIGNAL("atzero")
with SIGNAL("atzero(int)")
checkzero
和在 Form.__init__
中,因为你声明它的方式,它没有参数.
Substitute SIGNAL("atzero")
with SIGNAL("atzero(int)")
both in checkzero
and in Form.__init__
, because the way you're declaring it, it carries no argument.
您在新式"中的代码,
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class ZeroSpinBox(QSpinBox):
zeros = 0
def __init__(self, parent=None):
super(ZeroSpinBox, self).__init__(parent)
self.valueChanged.connect(self.checkzero)
atzero = Signal(int)
def checkzero(self):
if self.value() == 0:
self.zeros += 1
self.atzero.emit(self.zeros)
class Form(QDialog):
def __init__(self, parent= None):
super(Form, self).__init__(parent)
dial = QDial()
dial.setNotchesVisible(True)
spinbox = ZeroSpinBox()
spinbox.setRange(0,200)
dial.setRange(0,200)
layout = QHBoxLayout()
layout.addWidget(dial)
layout.addWidget(spinbox)
self.setLayout(layout)
dial.valueChanged.connect(spinbox.setValue)
spinbox.valueChanged.connect(dial.setValue)
spinbox.atzero.connect(self.announce)
self.setWindowTitle("Signals and Slots Part 2")
@Slot(int)
def announce(self, zeros):
print "ZeroSpinBox has been at zero %d times" % zeros
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
这篇关于PySide 发出信号导致 python 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!