QTreeWidget and setItemWidget



  • My GUI requires a tree-view with lists inside that the user can interact with. So I've added a QTreeWidget in which every top level item has one child-item that I use setItemWidget on to add a QListWidget.
    However in the documentation it does say: "This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead."

    You can't exactly call a QListWidget static content, but why shouldn't you do this? And should I really change to QTreeView and subclass QItemDelegate instead?


  • Qt Champions 2016

    hi and welcome

    well its not that it will explode or be really bad to use
    ListWidget + setItemWidget
    but view+delegate is design for such use case
    and have far better performance once u have many items.

    something like
    http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html

    Also the views helps editing etc whereas listwidget+setItem is more raw for many operations.

    But you should just test it out with listwidget and if it dont live up to the specs,
    the view+delegate will provide.



  • @mrjj Thank you :)

    well its not that it will explode or be really bad to use

    Heh okay that's just what I wanted to know ;)
    It's just, it works pretty good like it currently is and I just think it would be a major hassle to change it all to work properly with view + delegate and I doubt it would improve the performance much at all because my lists won't contain more than ~20 items at a time.

    I have another question though because I'm not quite sure if my program might have a memory leak.
    I have two tabs, when I click on a tab I want to delete all the items in the treewidget and then add the new items depending on which tab I pressed.
    This is an example of how I add my ListWidgets:

    QTreeWidgetItem *itemToplevel = new QTreeWidgetItem();
    itemTopLevel->setText(0, "Top level item");
    
    QTreeWidgetItem *itemListContainer = new QTreeWidgetItem();
    itemTopLevel->addChild(itemListContainer);
    
    ui->treeWidget->addTopLevelItem(itemTopLevel);
    
    QListWidget *list = new QListWidget();
    ui->treeWidget->setItemWidget(itemListContainer, 0, list);
    

    And then I add items to that list. Now, when I want to delete everything again before doing this again for the other tab, I call:

    ui-treeWidget->clear();
    

    In the documentation for the QTreeWidget for setItemWidget it says:

    Note: The tree takes ownership of the widget.

    So by calling clear on the tree widget I expect it to delete the list I've added (Including its items) and then of course the top-level items and their childs.
    Is this correct? Or am I responsible for any of it?


  • Qt Champions 2016

    hi
    well with 20 items, all should be golden. Also on desktop class pcs, it takes many rows before it get heavy, but say you target a
    mobile platform, it might be far less.

    The list owns the items+any widget and will delete them so you need not to care for it.

    One exception is if you use the setData function and give it pointer to some class.
    It will not delete such class.
    I mention this as the setData/data function is very handy to add info to items, say to map it back to some data structure so
    often used.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.