import os
import sys
from PyQt4.QtSql import *
from PyQt4.QtGui import *
db=None
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class zzNode(object):
def __init__(self, parent, uid, txt):
super(zzNode, self).__init__()
self.parent=parent
self.uid=uid
self.txt=txt
self.child=[]
cursor=db.exec_("select * from treetest where parent_uid=%s" % self.uid)
while cursor.next():
childId=int(cursor.record().field("uid").value().toString())
self.child.append(zzNode(self, childId, cursor.record().field("text").value().toString()))
def childCount(self):
return len(self.child)
class TreeOfTableModel(QAbstractItemModel):
def __init__(self, parent=None):
super(TreeOfTableModel, self).__init__(parent)
self.columns = 2
self.headers = ["key", "value"]
self.rootItem=zzNode(None, 0, "root")
def rowCount(self, parent):
if parent.column() > 0: return 0
if not parent.isValid():
parentItem = self.rootItem
else:
parentItem = parent.internalPointer()
return parentItem.childCount()
def columnCount(self, parent):
return self.columns
def data(self, index, role):
if role == Qt.DisplayRole:
tmp=index.internalPointer().child
if index.column()==0:
return index.internalPointer().uid
else:
return QVariant(index.internalPointer().txt)
else:
return QVariant()
def headerData(self, section, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(self.headers[section])
return QVariant()
def index(self, row, column, parent):
if not self.hasIndex(row, column, parent):
return QModelIndex()
if not parent.isValid():
parentItem = self.rootItem
else:
parentItem = parent.internalPointer()
childItem = parentItem.child[row]
if childItem:
return self.createIndex(row, column, childItem)
else:
return QtCore.QModelIndex()
def parent(self, child):
node = child.internalPointer()
if node is None:
return QModelIndex()
parent = node.parent
if parent is None:
return QModelIndex()
grandparent = parent.parent
if grandparent is None:
return QModelIndex()
row = grandparent.child.index(parent)
return self.createIndex(row, 0, parent)
class TreeOfTableWidget(QTreeView):
def __init__(self):
super(TreeOfTableWidget, self).__init__()
self.setSelectionBehavior(QTreeView.SelectItems)
self.setUniformRowHeights(True)
model = TreeOfTableModel(self)
self.setModel(model)
class MainForm(QMainWindow):
def __init__(self):
super(MainForm, self).__init__()
self.treeWidget = TreeOfTableWidget()
self.setCentralWidget(self.treeWidget)
QShortcut(QKeySequence("Escape"), self, self.close)
self.setWindowTitle("TreeViewExample")
app = QApplication(sys.argv)
db=QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(":memory:")
db.open()
db.exec_("create table treetest (uid int primary key, parent_uid int , text char)")
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (1, 0, "z01"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (2, 0, "z02"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (3, 0, "z03"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (4, 1, "z00-4"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (5, 1, "z00-5"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (6, 2, "z00-6"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (7, 2, "z01-7"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (8, 7, "z01-8"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (9, 7, "z02-9"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (10, 3, "z03-10"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (11, 10, "z03-11"))
db.exec_("insert into treetest (uid,parent_uid,text) values (%s,%s,'%s')" % (12, 11, "z03-12"))
db.commit()
form = MainForm()
form.resize(750, 550)
form.show()
app.exec_()