pythonuser-interfacepyqtqcomboboxqstringlistmodel

Error in model view implemention of GUI in pyqt


the following sample code is crashing with this error when I close the application:

QBasicTimer::start: QBasicTimer can only be used with threads started with QThread

here is my code :

import sys
from PyQt4 import QtGui ,QtCore

app = QtGui.QApplication(sys.argv)

data=[]
data.append("one")
model=QtGui.QStringListModel(data)

combobox=QtGui.QComboBox()
combobox.show()
combobox.setModel(model)

sys.exit(app.exec_())

I found out that's about using model but I don't know how to fix it.

edited: os : win 7 64bit pyqt4


Solution

  • The program is not "crashing": it is merely printing an error message during the normal shutdown process.

    The reason the message is being shown is a side-effect of garbage-collection. When python shuts down, the order in which objects get deleted can be unpredictable. This may result in objects on the C++ side being deleted in the "wrong" order, and so Qt will sometimes complain when this happens.

    One way to "fix" the sample code would be to simply rename some of the PyQt objects. If I change the name combobox to combo, for instance, the error message goes away. There's nothing mysterious about this - it just changes the order in which the objects are deleted.

    But another, much more robust, way to fix the problem would be to make sure the QStringListModel has a parent, since it's possible that Qt doesn't take ownership of it when it's passed to the combo-box. Qt should always handle the deletion of child objects correctly when they're linked together in this way. So the code example would become:

    import sys
    from PyQt4 import QtGui, QtCore
    
    app = QtGui.QApplication(sys.argv)
    
    combobox = QtGui.QComboBox()
    
    data = []
    data.append("one")
    model = QtGui.QStringListModel(data, combobox)
    combobox.setModel(model)
    
    combobox.show()
    
    sys.exit(app.exec_())