[PyQt] Segfault with PyQt

Alexandre Badez alexandre.badez at gmail.com
Thu Jan 17 16:44:47 GMT 2008


Skipped content of type multipart/alternative-------------- next part --------------
#! /bin/env python
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
import os
import time

lProb = []

class myModel(QtCore.QAbstractItemModel):
    def __init__(self, parent=None):
        self._myDataStruct = None
        super(myModel, self).__init__(parent)

    def setDataStruct(self, myDataStruct):
        self._myDataStruct = myDataStruct

    def columnCount(self, parent=None):
        return 1

    def data(self, index, role):
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            data = QtCore.QVariant(str(self.getMyObjectFromIndex(index)))
        else:
            data = QtCore.QVariant()        
        return data

    def flags(self, index):
        if not index.isValid():
            flags = QtCore.Qt.ItemIsEnabled
        else:
            flags = QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
        return flags

    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            v = QtCore.QVariant("header")
        else:
            v = QtCore.QVariant()
        return v

    def index(self, row, column, parent):
        if (parent is None) or (not parent.isValid()):
            parentNode = self._myDataStruct
        else:
            parentNode = self._myDataStruct.getMyObjectFromIndex(parent)
            if parentNode is None:
                parentNode = self._myDataStruct

        try:
            childNode = parentNode[row]
        except IndexError:
            childNode = None
        
        if childNode is not None:
            return self.createIndex(row, column, id(childNode))
        else:
            return QtCore.QModelIndex()

    def parent(self, index):
        parentIndex = None
        if not index.isValid():
            parentIndex = QtCore.QModelIndex()
        else:
            childNode = self.getMyObjectFromIndex(index)
            if childNode is None:
                parentIndex = QtCore.QModelIndex()
            else:
                parentIndex = self.getIndexFromMyObject(self._myDataStruct)
        return parentIndex

    def rowCount(self, parent):
        if not parent.isValid():
            node = self._myDataStruct
        else:
            node = self.getMyObjectFromIndex(parent)
        return len(node)

    def getIndexFromMyObject(self, myObject):
        try:
            dataIndex = self._myDataStruct.getIndex(myObject)
        except ValueError:
            index = QtCore.QModelIndex()
        else:
            index = self.createIndex(dataIndex, 0, id(myObject))
        return index

    def getMyObjectFromIndex(self, index):
        return self._myDataStruct.getData(index.internalId())

    def crashMethod11(self):
        QtGui.QDialog().exec_()
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        self.endRemoveRows()
        return True

    def crashMethod12(self):
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        QtGui.QDialog().exec_()
        self.endRemoveRows()
        return True

    def crashMethod13(self):
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        self.endRemoveRows()
        QtGui.QDialog().exec_()
        return True

    def crashMethod21(self):
        QtGui.QDialog().exec_()
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        QtGui.QDialog().exec_()
        self.endRemoveRows()
        return True

    def crashMethod22(self):
        QtGui.QDialog().exec_()
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        self.endRemoveRows()
        QtGui.QDialog().exec_()
        return True

    def crashMethod23(self):
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        QtGui.QDialog().exec_()
        self.endRemoveRows()
        QtGui.QDialog().exec_()
        return True

    def crashMethod3(self):
        QtGui.QDialog().exec_()
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        QtGui.QDialog().exec_()
        self.endRemoveRows()
        QtGui.QDialog().exec_()
        return True

class myDataStruct(object):
    def __init__(self):
        self._data = []
        self._dico = {}

    def __str__(self):
        return str(self._dico)
    
    def __iter__(self):
        return self._data.__iter__()

    def __getitem__(self, index):
        return self._data[index]
    
    def __len__(self):
        return len(self._data)

    def addData(self, myObjectToStore):
        self._data.append(myObjectToStore)
        self._dico[id(myObjectToStore)] = myObjectToStore
        
    def getData(self, id_):
        return self._dico[id_]
    
    def getIndex(self, myObject):
        return self._data.index(myObject)

class myObjectStored(object):
    def __init__(self):
        self._data = None

    def __len__(self):
        return 0

    def setData(self, data):
        self._data = data
    
    def __str__(self):
        return self._data

class myTest(object):
    def __init__(self):
        self._data = myDataStruct()
        self._model = None
        self._proxy = None

    def _initModelAndProxy(self):
        self._model = myModel()
        self._model.setDataStruct(self._data)
        
        self._proxy = QtGui.QSortFilterProxyModel()
        self._proxy.setSourceModel(self._model)

    def _closeModelAndProxy(self):
        del self._model
        del self._proxy

    def addObjectToData(self):
        obj = myObjectStored()
        obj.setData("obj "+ str(id(obj)))
        self._data.addData(obj)

    def testAllView(self):
        self._testViewWithMethod("crashMethod11")
        self._testViewWithMethod("crashMethod12")
        self._testViewWithMethod("crashMethod13")
        self._testViewWithMethod("crashMethod21")
        self._testViewWithMethod("crashMethod22")
        self._testViewWithMethod("crashMethod23")
        self._testViewWithMethod("crashMethod3")

    def _testViewWithMethod(self, crashMethodName):
        print "**********************************\n\t\tTest All View with:"
        for d in self._data:
            print "\t%(name)s\t%(id)d" % {
                'name' : str(d),
                'id' : id(d)}
        print "\t\tand method: ", crashMethodName
        self._testView(self._getListView, crashMethodName)
        self._testView(self._getTableView, crashMethodName)
        self._testView(self._getTreeView, crashMethodName)

    def _getListView(self):
        return QtGui.QListView, "View"

    def _getTableView(self):
        return QtGui.QTableView, "Table"

    def _getTreeView(self):
        return QtGui.QTreeView, "Tree"

    def _testView(self, factory, crashMethodName):
        clsView, name = factory()
        
        print name
        self._initModelAndProxy()
        view = clsView()
        print "\tDirect: ", str(self._testViewDirect(view, crashMethodName))
        view.close()
        self._closeModelAndProxy()

        self._initModelAndProxy()
        view = clsView()
        print "\tProxy: ", str(self._testViewProxy(view, crashMethodName))
        view.close()
        self._closeModelAndProxy()

    def _testViewDirect(self, view, crashMethodName):
        view.setModel(self._model)
        view.show()
        method = getattr(self._model, crashMethodName)
        if method():
            out = "OK"
        else:
            out = "NOK"
        return out

    def _testViewProxy(self, view, crashMethodName):
        view.setModel(self._proxy)
        view.show()
        method = getattr(self._model, crashMethodName)
        if method():
            out = "OK"
        else:
            out = "NOK"
        return out

app = QtGui.QApplication(sys.argv)

mt = myTest()

mt.testAllView()

mt.addObjectToData()
mt.testAllView()

sys.exit(app.exec_())
-------------- next part --------------
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
import os

class myModel(QtCore.QAbstractItemModel):
    def __init__(self, parent=None):
        self._myDataStruct = None
        super(myModel, self).__init__(parent)

    def setDataStruct(self, myDataStruct):
        self._myDataStruct = myDataStruct

    def columnCount(self, parent=None):
        return 1

    def data(self, index, role):
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            data = QtCore.QVariant(str(self.getMyObjectFromIndex(index)))
        else:
            data = QtCore.QVariant()        
        return data

    def flags(self, index):
        if not index.isValid():
            flags = QtCore.Qt.ItemIsEnabled
        else:
            flags = QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
        return flags

    def headerData(self, section, orientation, role):
        return QtCore.QVariant("header")

    def index(self, row, column, parent):
        if (parent is None) or (not parent.isValid()):
            parentNode = self._myDataStruct
        else:
            parentNode = self._myDataStruct.getMyObjectFromIndex(parent)
            if parentNode is None:
                parentNode = self._myDataStruct

        childNode = parentNode[row]
        if childNode is not None:
            return self.createIndex(row, column, id(childNode))
        else:
            return QtCore.QModelIndex()

    def parent(self, index):
        parentIndex = None
        if not index.isValid():
            parentIndex = QtCore.QModelIndex()
        else:
            childNode = self.getMyObjectFromIndex(index)
            if childNode is None:
                parentIndex = QtCore.QModelIndex()
            else:
                parentIndex = self.getIndexFromMyObject(self._myDataStruct)
        return parentIndex

    def rowCount(self, parent):
        if not parent.isValid():
            node = self._myDataStruct
        else:
            node = self.getMyObjectFromIndex(parent)
        return len(node)

    def getIndexFromMyObject(self, myObject):
        return self.createIndex(
            self._myDataStruct.getIndex(myObject),
            0,
            id(myObject))

    def getMyObjectFromIndex(self, index):
        return self._myDataStruct.getData(index.internalId())

    def methodFailed(self):
        QtGui.QDialog().exec_()
        print "first dialog ok"
        
        self.beginRemoveRows(QtCore.QModelIndex(), 0, 0)
        QtGui.QDialog().exec_()
        print "second dialog ok"

        self.endRemoveRows()
        QtGui.QDialog().exec_()
        print "third dialog ok"


class myDataStruct(object):
    def __init__(self):
        self._data = []
        self._dico = {}
    
    def __iter__(self):
        self._data.__iter__()

    def __getitem__(self, index):
        return self._data[index]
    
    def __len__(self):
        return len(self._data)

    def addData(self, myObjectToStore):
        self._data.append(myObjectToStore)
        self._dico[id(myObjectToStore)] = myObjectToStore
        
    def getData(self, id_):
        return self._dico[id_]
    
    def getIndex(self, myObject):
        return self._data.index(myObject)

class myObjectStored(object):
    def __init__(self):
        self._data = None

    def __len__(self):
        return 0

    def setData(self, data):
        self._data = data
    
    def __str__(self):
        return self._data



app = QtGui.QApplication(sys.argv)

Data = myDataStruct()

obj1 = myObjectStored()
obj1.setData("obj 1")
Data.addData(obj1)

model = myModel()
model.setDataStruct(Data)

proxy = QtGui.QSortFilterProxyModel()
proxy.setSourceModel(model)

#view = QtGui.QTableView() # <- Work
#view = QtGui.QListView() # <- Work
view = QtGui.QTreeView() # <- Crash
view.setModel(proxy)

view.show()

model.methodFaile()

obj2 = myObjectStored()
obj2.setData("obj 2")
Data.addData(obj2)

model.methodFaile()

sys.exit(app.exec_())


More information about the PyQt mailing list