tags:

views:

230

answers:

2

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?

A: 

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
Andre Miller
Result is the same but i modify my code to make that clear.
kraymer
A: 

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?

Alex Martelli
Python 2.6.2 from python.org and PyQt 4.5.2Just switched back to Python 2.5.4 and it works like a charm. Thanks!
kraymer
@kraymer, you're welcome, but it's troublesome if the latest PyQt doesn't work with the latest Python -- when I have time I'll try to rebuild PyQt with Python 2.6.2 and see what gives (maybe open a bug on PyQt's tracker...?)
Alex Martelli