<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
Tried with Qt 4.3, Qt 4.4, PyQt 4.3.3, PyQt 4.4. I think it has
something to do with my parent implementation. It starts ok but if you
try to open the tree it segfaults right away.<br>
<br>
#!/bin/env python<br>
import sys<br>
<br>
from PyQt4 import QtCore, QtGui<br>
from PyQt4.QtCore import SIGNAL<br>
<br>
EMPTY_VARIANT = QtCore.QVariant()<br>
EMPTY_INDEX = QtCore.QModelIndex()<br>
<br>
class Item:<br>
def __init__(self,name,parent=None):<br>
self._children = []<br>
self.name = name<br>
self.parent = parent<br>
def children(self):<br>
return self._children<br>
<br>
root = Item("root")<br>
for i in range(1,10):<br>
root._children.append(Item("item%d" % i,root))<br>
<br>
child = root._children[0]<br>
for i in range(1,3):<br>
child._children.append(Item("citem%d" % i,child))<br>
<br>
class SimpleTreeModel(QtCore.QAbstractItemModel):<br>
def __init__(self, parent=None):<br>
QtCore.QAbstractItemModel.__init__(self, parent)<br>
self.elements = root<br>
<br>
def data(self, index, role):<br>
if not index.isValid():<br>
return EMPTY_VARIANT<br>
<br>
e = index.internalPointer()<br>
<br>
if role == 0:<br>
return QtCore.QVariant(e.name)<br>
<br>
return EMPTY_VARIANT<br>
<br>
def index(self, row, column, parent):<br>
if parent.isValid():<br>
item = parent.internalPointer()<br>
else:<br>
item = self.elements<br>
return self.createIndex(row, column, item.children()[row])<br>
<br>
def parent(self, index):<br>
if not index.isValid():<br>
return EMPTY_INDEX<br>
item = index.internalPointer()<br>
if not item.parent:<br>
return EMPTY_INDEX<br>
<br>
pp = item.parent.parent<br>
if not pp:<br>
c = self.elements.children()<br>
else:<br>
c = pp.children()<br>
try:<br>
i = c.index(item.parent)<br>
except ValueError:<br>
i = 0<br>
return self.createIndex(i,0,item.parent)<br>
<br>
def rowCount(self, parent):<br>
if parent.isValid():<br>
item = parent.internalPointer()<br>
return len(item.children())<br>
else:<br>
print len(self.elements.children())<br>
return len(self.elements.children())<br>
<br>
def columnCount(self, parent):<br>
return 1<br>
<br>
<br>
if __name__ == "__main__":<br>
app = QtGui.QApplication(sys.argv)<br>
<br>
view = QtGui.QTreeView()<br>
model = SimpleTreeModel(view)<br>
view.setModel(model)<br>
view.show()<br>
app.exec_()<br>
<br>
<br>
<br>
<div class="moz-signature">-- <br>
-Matt<br>
<br>
<span style="font-size: 11px;"><a
href="http://twiki.spimageworks.com/twiki/bin/view/Software/CueDevelopment"><br>
</a></span><span style="font-size: 11px;"><a
href="http://twiki.spimageworks.com/twiki/bin/view/Software/MiddleTier"></a><span></span></span></div>
</body>
</html>