Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget and setItemWidget
Qt 6.11 is out! See what's new in the release blog

QTreeWidget and setItemWidget

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DenjasD Offline
    DenjasD Offline
    Denjas
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      2
      • DenjasD Offline
        DenjasD Offline
        Denjas
        wrote on last edited by Denjas
        #3

        @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?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved