Results 1 to 4 of 4

Thread: How to set widgets as children items on QTreeView

  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:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to set widgets as children items on QTreeView

    Quote Originally Posted by q130s View Post
    Is it possible to add widgets to the children items on QTreeView?
    Yes and no. Yes, because you can use setIndexWidget(), no because it only puts the widget in the view but has no relation to items of the view (or model). And you really want to avoid it for performance reasons. If you want only a button then you can implement it with a custom delegate. If you want something more than a button then I you might want to use QScrollArea with real widgets instead of a view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:


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

    Default Re: How to set widgets as children items on QTreeView

    I figured out how to add widget to children. Tricky enough, using QStandardItem.insertRow combined with index does the work. In my sample code above, replace `_add_widget` with the following:

    Qt Code:
    1. def _add_widget(self, n):
    2. item_toplevel = QStandardItem('{}th item'.format(n))
    3. self._datamodel.setItem(n, 0, item_toplevel)
    4.  
    5. widget_toplevel = QPushButton('{}th button'.format(n))
    6. qindex_toplevel = self._datamodel.index(n, 1, QModelIndex())
    7. self.setIndexWidget(qindex_toplevel, widget_toplevel)
    8.  
    9. if n == 2:
    10. item_child_col0 = QStandardItem('child col0')
    11. item_child_col1 = QStandardItem('child col1')
    12. #item_toplevel.appendRow(item_child_col0)
    13.  
    14. item_toplevel.insertRow(0, [item_child_col0, item_child_col1])
    15.  
    16. widget_child = QPushButton('child widget')
    17. qindex_child = item_child_col1.index()
    18. self.setIndexWidget(qindex_child, widget_child)
    To copy to clipboard, switch view to plain text mode 

    I'm sure this is NOT the best/ideal designed way but seems to work for me.

    tmAdr.png


    Added after 5 minutes:


    @wysota thanks for the reply. Nice to know the reasoning behind setIndexWidget(). However I was asking about how I can add widget as "CHILDREN" to QTreeView (or I might better say "How can I use widgets as hierarchical tree nodes on QTreeView"). I managed to figure out as I posted it already. Any thoughts about it is appreciated!
    Last edited by q130s; 9th March 2013 at 23:23.

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


  7. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to set widgets as children items on QTreeView

    Quote Originally Posted by q130s View Post
    However I was asking about how I can add widget as "CHILDREN" to QTreeView (or I might better say "How can I use widgets as hierarchical tree nodes on QTreeView"). I managed to figure out as I posted it already. Any thoughts about it is appreciated!
    Actually you were asking about how to add widgets as children of the view's viewport. And that's what setIndexWidget does. Which is what you shouldn't do as I already said. You have 5 items here. Try doing that for 5000 items.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota 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.