Results 1 to 4 of 4

Thread: How to set widgets as children items on QTreeView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to set widgets as children items on QTreeView

    Is it possible to add widgets to the children items on QTreeView?

    Thanks to [this thread on stackoverflow](http://stackoverflow.com/questions/9...setindexwidget), I'm able to add widgets to 2nd or later column of `QAbstractItemView` (in my example QTreeView of top level items of a view.

    Here's what I've tried which partly went well:

    Qt Code:
    1. #!/usr/bin/env python
    2. import os
    3.  
    4. from PyQt4.QtCore import QModelIndex, Qt
    5. from PyQt4.QtGui import QApplication, QItemSelectionModel, \
    6. from PyQt4.uic import loadUi
    7.  
    8.  
    9. class PrvTreeviewNest(QTreeView):
    10. def __init__(self):
    11. super(PrvTreeviewNest, self).__init__()
    12.  
    13. loadUi('/home/user/yourproject/resource/treeview_nest.ui')
    14.  
    15. # row can be 0 even when it's more than 0.
    16. self._datamodel = QStandardItemModel(0, 2)
    17. self.setModel(self._datamodel)
    18.  
    19. for i in range(4):
    20. self._add_widget(i + 1)
    21.  
    22. self.show()
    23.  
    24. def _add_widget(self, n):
    25. std_item = QStandardItem('{}th item'.format(n))
    26. self._datamodel.setItem(n, 0, std_item)
    27.  
    28. node_widget = QPushButton('{}th button'.format(n))
    29. qindex_widget = self._datamodel.index(n, 1, QModelIndex())
    30. self.setIndexWidget(qindex_widget, node_widget)
    31.  
    32. if n == 2:
    33. std_item_child = QStandardItem('child')
    34. std_item.appendRow(std_item_child)
    35.  
    36. node_widget_child = QPushButton('petit button')
    37. qindex_widget_child = self._datamodel.index(n, 1, QModelIndex())
    38. self.setIndexWidget(qindex_widget_child, node_widget_child)
    39.  
    40. if __name__ == '__main__':
    41. import sys
    42. app = QApplication(sys.argv)
    43. window = PrvTreeviewNest()
    44. window.resize(320, 240)
    45. window.setWindowTitle(
    46. QApplication.translate("toplevel", "Top-level widget"))
    47.  
    48. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    [treeview_nest.ui](http://pastebin.com/G0DhQay3) is available.

    You see in the image below that the item `child` doesn't show a button and its parent's button is overwritten. Apparently I have no idea how to write a code for it.
    TPsZ9.png

  2. The following user says thank you to q130s for this useful post:


Similar Threads

  1. Transparent widget and their children widgets
    By naturalpsychic in forum Qt Programming
    Replies: 4
    Last Post: 18th November 2012, 09:08
  2. QTreeView children with multiple coulms
    By maxpower in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2012, 14:10
  3. Hide children of QStandardItem in QTreeView
    By Phlucious in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2012, 20:48
  4. QTreeView adding children to the view
    By Tux-Slack in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2007, 09:28
  5. QTreeView and QStandardModel : add children
    By Valheru in forum Newbie
    Replies: 7
    Last Post: 19th September 2006, 17:24

Tags for this Thread

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.