LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2013, 07:19 PM   #1
redfox2807
Member
 
Registered: Jul 2012
Distribution: Debian testing/stable, Gentoo, CentOS 7, Sailfish OS, Android
Posts: 164

Rep: Reputation: 29
QTreeView & inherited QAbstractItemModel doesn't display anything


Hey guys,

I'm trying to learn QTreeView and getting stuck. This is slightly altered code from Qt's Simple Tree Model Example converted to Python. When I run the script the treeView doesn't display anything but the header. Still treeModel.data() returns the right strings. What's wrong with the code?

Code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class TreeItem:
    def __init__(self, data, parent = None):
        self.parentItem = parent
        if type(data) == list:
            self.itemData = data
        else:
            self.itemData = [data]
        self.childItems = []
        # -1 for root item
        if parent == None:
            self.row = -1

    def appendChild(self, item):
        item.row = len(self.childItems)
        self.childItems.append(item)

    def row(self):
        return self.row

    def columnCount(self):
        return(len(self.itemData))

    def parent(self):
        return self.parent

    def child(self, row):
        if row < len(self.childItems):
            return self.childItems[row]
        else:
            return None
    def childCount(self):
        return len(self.childItems)

    def data(self, column):
        if column < len(self.itemData):
            return self.itemData[column]
        else:
            return None

class TreeModel(QAbstractItemModel):
    def __init__(self, parent = None):
        QAbstractItemModel.__init__(self, parent)
        self.rootItem = TreeItem(["Title"], None)
        self.populateModel()

    def index(self, row, column, parent = 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 QModelIndex()

    def parent(self, index):
        if not index.isValid():
            return QModelIndex()
        childItem = index.internalPointer()
        parentItem = childItem.parent()
        if parentItem == rootItem:
            return QModelIndex()
        return self.createIndex(parentItem.row(), 0, parentItem)

    def columnCount(self, parent = QModelIndex()):
        if parent.isValid():
            return parent.internalPointer().columnCount()
        else:
            return self.rootItem.columnCount()

    def rowCount(self, parent = QModelIndex()):
        if parent.column():
            return 0
        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()
        return parentItem.childCount()

    def headerData(self, section, orientation, role = Qt.DisplayRole):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.rootItem.data(section)
        return None

    def flags(self, index):
        if not index.isValid():
            return Qt.NoItemFlags
        return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable

    def data(self, index, role):
        if not index.isValid():
            return None
        if role != Qt.DisplayRole:
            return None
        item = index.internalPointer()
        return item.data(index.column())

    def appendChild(self, item, parent = None):
        if not parent or not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()
        parentItem.appendChild(item)

    def populateModel(self):
        items = ["Good", "Better", "Best"]
        for i, str in enumerate(items):
            treeItem = TreeItem([str])
            self.appendChild(treeItem)
        items = ["Never", "Rest"]
        index = self.index(0, 0, QModelIndex())
        for i, str in enumerate(items):
            self.appendChild(TreeItem([str], treeItem), index)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    treeView = QTreeView()
    treeModel = TreeModel()
    treeView.setModel(treeModel)
    treeView.show()
    index = treeModel.index(0,0)
    print(treeModel.data(index, Qt.DisplayRole))
    index = treeModel.index(1,0, index)
    print(treeModel.data(index, Qt.DisplayRole))
    sys.exit(app.exec_())
 
Old 03-30-2013, 10:29 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,497

Rep: Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523Reputation: 3523
Your TreeModel.rowCount() is returning 0. Remove the first two lines:

Code:
if parent.column():
    return 0
 
1 members found this post helpful.
Old 03-31-2013, 12:33 AM   #3
redfox2807
Member
 
Registered: Jul 2012
Distribution: Debian testing/stable, Gentoo, CentOS 7, Sailfish OS, Android
Posts: 164

Original Poster
Rep: Reputation: 29
Thanks so much. Now it works.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why doesn't XMMS display artists & song titles, when i'm listenig to internet radio? Mistreated Linux - Software 7 08-05-2005 01:51 PM
Hard drives start at hdc && fdisk doesn't show hde triet Linux - Hardware 0 11-03-2004 10:25 PM
sound & ethernet & video display redhatuser Linux - Networking 0 08-11-2004 09:56 AM
Mandrake 9.1/IOGear ION External USB 2.0 hdd && Laptop LCD Display Prismatic Linux - Hardware 8 02-09-2004 09:17 PM
linux config & installation & display card problem spsonar Linux - Software 0 02-14-2002 07:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration