PyQt : why adding a dummy class definition in my file make the application crash?
Question
Consider the code below:
#!/usr/bin/env python
from PyQt4 import QtCore, QtGui
import os,sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.listWidget = QtGui.QListWidget(None)
self.setCentralWidget(self.listWidget)
if __name__ == '__main__':
app = QtGui.QApplication (sys.argv)
mainWin = MainWindow ()
mainWin.show ()
sys.exit (app.exec_())
Works ok. Now if I add a dummy class (that inherits from a QtGui module's class) in the global scope ...
class MainWindow(QtGui.QMainWindow):
... # unchanged
class MyWidget(QtGui.QWidget):
def __init__(self):
super(MyWidget, self).__init__()
if __name__ == '__main__':
... # unchanged
... when i launch the script i get the error:
TypeError: argument 1 of QMainWindow.setCentralWidget() has an invalid type
This error message is cryptic for me as i can't connect it to the modification done. Do you have an idea what could be the source of this error?
Solution
Can't reproduce the problem as reported: the following exact code
from PyQt4 import QtCore, QtGui
import os, sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.listWidget = QtGui.QListWidget(None)
self.setCentralWidget(self.listWidget)
class MyWidget(QtGui.QWidget):
def __init__(self):
super(MyWidget, self).__init__()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
runs just fine for me (showing an empty window of course). So I guess it's down to versions details! I'm using system-supplied Python 2.5.1 on Mac OS X 10.5.7 and adding a
print QtCore.PYQT_VERSION_STR
shows I'm on version 4.5.1 of PyQt. What about you?
OTHER TIPS
I have not worked with PyQt before, but didn't you forget to call the constructor of the superclass here?
class MyWidget(QtGui.QWidget):
def __init__(self):
# Where is the call to QtGui.QWidget's init ?
pass