Results 1 to 10 of 10

Thread: Nesting TableViews under TreeView items

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Nesting TableViews under TreeView items

    So the idea is to be able to expand and minimize items that contain 1 table each. The items may have children of their own, each of which may also have 1 table each.

    My spontaneous solution would be to use a treeview with the parent item being a standard AbstractItem, but the child would be something that a custom QStyledItemDelegate could pick up on to draw a TableView in it's place.

    I'm with PyQt but could read most languages I suppose.

    Adding an image that may help clarify what I mean.
    nestedTables.jpg

    Thanks,
    Marcus


    Added after 26 minutes:


    Guess a question might be appropriate here.. sorry, it's late.

    So, how would you guys do it?
    Last edited by marcuso; 23rd March 2012 at 23:04.

  2. #2
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Nesting TableViews under TreeView items

    This is pretty close to what I'm looking for. Let me know if there are any drawbacks to the way this works.

    Qt Code:
    1. import sys
    2.  
    3. from PyQt4.QtGui import *
    4. from PyQt4.QtCore import *
    5.  
    6. class EditorDelegate(QStyledItemDelegate):
    7. def createEditor(self, parent, option, index):
    8. if index.column() == 0:
    9. #item = index.internalPointer()
    10.  
    11. table = QTableWidget(parent)
    12. table.insertRow(0)
    13. table.insertRow(1)
    14. table.insertColumn(0)
    15.  
    16. item1 = QTableWidgetItem()
    17. item1.setText('item1')
    18. table.setItem(0, 0, item1)
    19.  
    20. return table
    21.  
    22. else:
    23. return QStyledItemDelegate.createEditor(self, parent, option, index)
    24.  
    25.  
    26. app = QApplication(sys.argv)
    27.  
    28. delegate = EditorDelegate()
    29.  
    30. tree = QTreeWidget()
    31. tree.setItemDelegate(delegate)
    32.  
    33. item1 = QTreeWidgetItem(tree)
    34. item1.setText(0, 'item1')
    35. tableChild = QTreeWidgetItem(item1)
    36. tableChild.setText(0, 'table')
    37.  
    38. item2 = QTreeWidgetItem(item1)
    39. item2.setText(0, 'item2')
    40. tableChild2 = QTreeWidgetItem(item2)
    41. tableChild2.setText(0, 'table2')
    42.  
    43. tree.openPersistentEditor(tableChild)
    44. tree.openPersistentEditor(tableChild2)
    45.  
    46. tree.show()
    47.  
    48. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    Getting the internalPointer from the item I'm creating an editor from causes a crash. My understanding is very limited at this point and so I fail to see why. Ideally, I'd like to query an attribute of the tree node and only draw the editor on item's that are designed to have one. I can however live with manually specifying the persistentEditor state on items I wan't to have editable and simply have all other items not be able to enter or go out of an editable state since the tableViews are supposed to always stay open.

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

    Default Re: Nesting TableViews under TreeView items

    Why don't you just use a QTreeView or QTreeWidget, potentially with custom drawing?
    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. #4
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Nesting TableViews under TreeView items

    Hey wysota, thanks for your reply.

    I've got very limited knowledge of custom drawing, could you please help clarify how that would help me in this case? What are the benefits of that compared to how I did it in the example posted above? Also, I was using a TreeView. How do you suggest I use it differently?

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

    Default Re: Nesting TableViews under TreeView items

    The benefits are quite obvious -- you get a working single view as opposed to what you have which is multiple views unrelated to each other placed inside yet another view again completely unrelated to anything. I would start by simply using QTreeView::setFirstColumnSpanned() to make the first column of the items you want to span the whole width of the view.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5. model.setColumnCount(2);
    6. model.setHorizontalHeaderLabels(QStringList() << "Key" << "Value");
    7. for(int i=0;i<10;++i) {
    8. QList<QStandardItem*> top;
    9. QStandardItem *topItem = new QStandardItem(QString("row %1").arg(i+1));
    10. top << topItem;
    11. model.appendRow(top);
    12. for(int j=0;j<5;++j) {
    13. QList<QStandardItem*> child;
    14. child << new QStandardItem(QString::number(qrand()%100)) << new QStandardItem(QString::number(qrand() % 100));
    15. topItem->appendRow(child);
    16. }
    17. }
    18. QTreeView view;
    19. view.setModel(&model);
    20. view.expandAll();
    21. for(int i=0;i<model.rowCount();++i) {
    22. view.setFirstColumnSpanned(i, QModelIndex(), true);
    23. }
    24. view.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    spannedtree.png

    The rest is just a matter of drawing according to your needs.
    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.


  6. #6
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Nesting TableViews under TreeView items

    I see. How about if I wish to edit the items in this TableView?

    What I have is a browser with items, and some of the items has properties stored within it along with children that may also have properties. What I'm looking to do is to visualize this data and also let the user edit it.

    So, one treeview for browsing the items, and another for showing the currently selected item along with it's properties and children with additional properties.

    I'm currently trying out making another custom AbstractItemModel for this Editor along with a QStyledItemDelegate attached to the View of that model. The model would be able to use the same data (being dragged and dropped from the browser onto the Editor) but display it according to the Delegate.

    Does this seem reasonable or is custom drawing still the way to go? Again, this is very new territory to me.

    Thanks for your help!

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

    Default Re: Nesting TableViews under TreeView items

    Quote Originally Posted by marcuso View Post
    I see. How about if I wish to edit the items in this TableView?
    Then edit them What's the big deal?

    I think you are making things more complex than they really are.
    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. #8
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Nesting TableViews under TreeView items

    I think you are right.

    But okay, I was getting used to having the benefits of a TableView, such as the default behaviour of the delegate to get spinboxes and such for values that are supposed to be numerical only, and the drawing of lines inbetween fields. Wouldn't I have to draw that myself if going with the custom drawing approach? Is there a middleground somewhere?

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

    Default Re: Nesting TableViews under TreeView items

    Quote Originally Posted by marcuso View Post
    But okay, I was getting used to having the benefits of a TableView, such as the default behaviour of the delegate to get spinboxes and such for values that are supposed to be numerical only
    You should be getting those for a tree view as well. It's using the same delegate.
    Wouldn't I have to draw that myself if going with the custom drawing approach?
    The default delegate doesn't draw the grid for a tree view. That's why I said you needed custom drawing but it's not a big issue. Certainly a smaller one than embedding a view in another 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.


  10. #10
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Nesting TableViews under TreeView items

    Okay. I've been experimenting a bit while chatting with you and it all sounds and looks really good. I'm gonna give this a go and get back to you. Thanks for your help.

Similar Threads

  1. Replies: 1
    Last Post: 4th August 2011, 22:36
  2. Replies: 1
    Last Post: 27th October 2010, 18:06
  3. Replies: 1
    Last Post: 2nd August 2010, 20:16
  4. Editing TableViews in UI Editor
    By emorgen in forum Newbie
    Replies: 2
    Last Post: 28th May 2010, 18:43
  5. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

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.