Results 1 to 5 of 5

Thread: PyQt4 tree model

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default PyQt4 tree model

    Qt Code:
    1. class BrickListModel(QAbstractItemModel):
    2.  
    3. def __init__(self):
    4. QAbstractItemModel.__init__(self)
    5.  
    6. self.bricks = []
    7.  
    8. def addBrick(self, brick):
    9. self.beginInsertRows(QModelIndex(), len(self.bricks), len(self.bricks))
    10. self.bricks.append(brick)
    11. self.endInsertRows()
    12.  
    13. def removeBrick(self, index):
    14. self.beginRemoveRows(QModelIndex(), index, index)
    15. del self.bricks[index]
    16. self.endRemoveRows()
    17.  
    18. def clearBricks(self):
    19. if len(self.bricks) != 0:
    20. self.beginRemoveRows(QModelIndex(), 0, len(self.bricks) - 1)
    21. self.bricks = []
    22. self.endRemoveRows()
    23.  
    24. def getAllBricks(self):
    25. return self.bricks
    26.  
    27. def index(self, row, column, parent):
    28. if not parent.isValid():
    29. if column < 2:
    30. if row > len(self.bricks):
    31. return createIndex(row, column)
    32. return QModelIndex()
    33.  
    34. def rowCount(self, parent):
    35. return len(self.bricks)
    36.  
    37. def columnCount(self, parent):
    38. return 2
    39.  
    40. def data(self, index, role):
    41. if index.column == 0:
    42. return self.bricks[index.row()]['id']
    43. elif index.column == 1:
    44. return self.bricks[index.row()]['comment']
    45. return QVariant()
    46.  
    47. def parent(self, index):
    48. return QModelIndex()
    49.  
    50. def headerData(self, section, orientation, role):
    51. if section == 0:
    52. return "Brick"
    53. if section == 1:
    54. return "Comment"
    55. return QVariant()
    To copy to clipboard, switch view to plain text mode 

    I am using a tree view instead of a list view because I want to have multiple columns.

    The problem is that no items show up in the view. What's the problem?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Angry Re: PyQt4 tree model

    Some comments for you to try:

    Qt Code:
    1. def index(self, row, column, parent):
    2. if not parent.isValid():
    3. # if column < 2: // The model has a columnCount function
    4. # if row > len(self.bricks): // When will this ever be true?
    5. return createIndex(row, column)
    6. return QModelIndex()
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. def data(self, index, role):
    2. if(role == Qt.DisplayRole): // add - only return data for display role
    3. if index.column() == 0: // add () after column
    4. return self.bricks[index.row()]['id']
    5. elif index.column() == 1: // add () after column
    6. return self.bricks[index.row()]['comment']
    7. return QVariant()
    To copy to clipboard, switch view to plain text mode 
    HTH

  3. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PyQt4 tree model

    It kind of works now, but the header is not shown and there is an arrow (like the one to view the item's children) on each line.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Wink Re: PyQt4 tree model

    Try putting the following in headerData():
    Qt Code:
    1. if(orientation == Qt.Horizontal and role == Qt.DisplayRole):
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by MTK358 View Post
    ... there is an arrow (like the one to view the item's children) on each line.
    Check out QTreeView::rootIsDecorated()

    BTW, I didn't mean to put the angry emoticon in the title of my first post.

  5. The following user says thank you to norobro for this useful post:

    MTK358 (24th February 2011)

  6. #5
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PyQt4 tree model

    It works great now!

Similar Threads

  1. SQL Tree Model Help
    By MTK358 in forum Newbie
    Replies: 9
    Last Post: 22nd June 2015, 15:02
  2. custom model + PyQt4
    By wirasto in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2009, 08:42
  3. Replies: 12
    Last Post: 5th July 2009, 16:03
  4. Writing a Tree model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2009, 11:22
  5. using the example of simple tree model
    By krishna.bv in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2006, 12:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.