[PyKDE] PyQt and trees

Bert Rodiers bert.rodiers at gmail.com
Mon Apr 10 21:15:15 BST 2006


On 4/10/06, Bert Rodiers <bert.rodiers at gmail.com> wrote:
> On 4/9/06, Andreas Pakulat <apaku at gmx.de> wrote:
>
> > Provide a minimal compilable example that demonstrates your problem. We
> > can't tell what's going on from the snippet above, at least I can't.
> >
>
> Ok, I'll do that (tomorrow). I'll have to reduce the amount of code.

In attachment an example for the first problem (the rest will
follow...). The tree should be filled in when I push the button. If I
put a breakpoint in the __init__ of class TreeModel it works,
otherwise it doesn't.

Bert
-------------- next part --------------
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created: Mon Apr 10 12:27:30 2006
#      by: PyQt4 UI code generator vsnapshot-20060407
#
# WARNING! All changes made in this file will be lost!

import sys
from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(QtCore.QSize(QtCore.QRect(0,0,800,600).size()).expandedTo(MainWindow.minimumSizeHint()))
        
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        
        self.treeView = QtGui.QTreeView(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(170,180,120,80))
        self.treeView.setObjectName("treeView")
        
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(320,80,75,24))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setGeometry(QtCore.QRect(0,581,800,19))
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"),self.test)
    
    def tr(self, string):
        return QtGui.QApplication.translate("MainWindow", string, None, QtGui.QApplication.UnicodeUTF8)
    
    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(self.tr("MainWindow"))
        self.pushButton.setText(self.tr("PushButton"))
    def test(self):
        model = TreeModel()
        self.treeView.setModel(model)



class TreeItem:
	def __init__(self, label, parent = None):
		self.parentItem = parent
		self.children = []
		self.label = label

	def child(self, row):
		return self.children[row]

	def childCount(self):
		return len(self.children)

	def displayLabel(self):
		return self.label

	def parent(self):
		return self.parentItem

	def row(self):
		if self.parentItem:
			return self.parentItem.children.index(self)
		return 0
	def dictionaryOfAllTreeElements(self):
		dictionary = {}
		for child in self.children:
			dictionary[id(child)] = child
			dictionary.update(child.dictionaryOfAllTreeElements())
		return dictionary
	
	def addChild(self, label):
		self.children.append(TreeItem(label, self))



class TreeModel(QtCore.QAbstractItemModel):
	def __init__(self, parent=None):
		QtCore.QAbstractItemModel.__init__(self, parent)


		self.rootItem = TreeItem('title')
		self.rootItem.addChild('1')
		self.rootItem.addChild('2')
		self.rootItem.children[0].addChild('3')
		self.rootItem.children[0].addChild('4')
		self.object_dict = self.rootItem.dictionaryOfAllTreeElements()
	
	def columnCount(self, parent):
		return 1
	
	def data(self, index, role):
		if not index.isValid():
			return QtCore.QVariant()

		if role != QtCore.Qt.DisplayRole:
			return QtCore.QVariant()

		item = self.object_dict[index.internalId()]
		label = item.displayLabel()
		
		assert(index.column() == 0)
		return QtCore.QVariant(label)

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

		return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

	def index(self, row, column, parent):
		if not parent.isValid():
			parentItem = self.rootItem
		else:
			parentItem = self.object_dict[parent.internalId()]

		childItem = parentItem.child(row)
		if childItem:
			return self.createIndex(row, column, id(childItem))
		else:
			return QtCore.QModelIndex()

	def parent(self, index):
		if not index.isValid():
			return QtCore.QModelIndex()

		childItem = self.object_dict[index.internalId()]
		parentItem = childItem.parent()

		if parentItem == self.rootItem:
			return QtCore.QModelIndex()

		return self.createIndex(parentItem.row(), 0, id(parentItem))

	def rowCount(self, parent):
		if not parent.isValid():
			parentItem = self.rootItem
		else:
			parentItem = self.object_dict[parent.internalId()]

		return parentItem.childCount()


if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	mainWindow = QtGui.QMainWindow()
	windowClass = Ui_MainWindow()
	windowClass.setupUi(mainWindow)
	app.setActiveWindow(mainWindow)
	mainWindow.show()
	app.exec_()	



More information about the PyQt mailing list