PySide emitting signal causes python to crash

I am working on the book "Rapid Gui Programming with Python and Qt" and I have a problem with the signal / slots project. I downloaded the authors code to compare with mine, and everything looks the same, however, when I emit a signal from the box derived class, python just crashes. Here is all the code 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_() 

My problem arises when the spinbox drops to zero, the checkzero (self) method (of the ZeroSpinBox class) is called, the line self.zeros + = 1 is fine, then on the lines of the emission line it says Python. exe crashed. The error I get is "python.exe stopped working" and the console reports "The process has completed exit code -1073741819"

Any idea why this is happening? These are Python 2.7.2 and PyQT4 w / PySide.

+1
source share
2 answers

Replace SIGNAL("atzero") with SIGNAL("atzero(int)") both checkzero and Form.__init__ because it declares no arguments as you declare it.

Edit: your code in the "new style",

 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_() 
+4
source

This is due to the difference between PySide and PyQt4, which is described here (in fact, this is an error in PySide - using an unsupported form of signal syntax should lead to an error, and not to an application crash).

The book you used was written specifically for PyQt4, so you probably need to know about the differences between PySide and PyQt4 when using it. For example, here .

Note that the PyQt4 version of your script works fine, with or without the bracketed part of the signal - all that matters is that they are the same. However, this is only true for custom signals - for predefined Qt signals and slots, you should always include the portion of the signature enclosed in brackets.

Another thing you should be aware of is that the syntax of the signal / slot you are using has been exceeded far more by the syntax of the new pythonic style. So, at some point it would be useful to read the manual here if your book does not cover it.

+2
source

Source: https://habr.com/ru/post/969486/


All Articles