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 AddChild causes Crash!
Forum Update on Monday, May 27th 2025

QTreeWidget AddChild causes Crash!

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.3k Views
  • 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.
  • C Offline
    C Offline
    Cysis145
    wrote on last edited by A Former User
    #1

    Hi guys,

    I'm trying to add a child QTreeWidgetItem to a pre-existing Item (which I added through the visual Editor). While I am able to get the correct selection of the item i want to be the parent, whenever i try adding any child items to it, builds fine but then crashes when launched.

    //header file
    QModelIndex InterIndex;
    
    //CPP file
    QTreeWidgetItem *newChild = new QTreeWidgetItem();
    newChild->setText(0, "i am a test Child!");
     
    //Select the correct pre-existsing item
    QItemSelectionModel *InterSel = new QItemSelectionModel(ui->TW_World->model());
    InterIndex = ui->TW_World->model()->index(5,0,QModelIndex());
    InterSel->select(InterIndex, QItemSelectionModel::Select);
    ui->TW_World->setSelectionModel(InterSel);
    
    ui->TW_World->currentItem()->addChild(newChild); // This line causes the crash
    

    Seems everywhere i look my code appears to be right but something is getting messed up, probably something really simple.

    Thanks!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Don't create new selection models every time you want to select something. QTreeWidget already has a selection model and you should use that.

      Second thing is that selected and current items are not the same thing. Programatically selecting an item doesn't make it current automatically.

      Third thing is that you have a crash because you never check return values. For example ui->TW_World->currentItem() can return nullptr if there's no current item and then you try to call a method on it, so it crashes.

      One more thing - QTreeWidget is a higher level abstraction over the QTreeView. When using it try not to go to the lower level i.e. don't use model(), QModelIndex or selection model directly. These are lower layer classes useful when using QTreeView directly or implementing your own model.

      Also - you don't need to mess with selection to add a child item at all. Those are two unrelated operations.

      So, to add a child to specific item you would do:

      QTreeWidgetItem* parent_item = ui->TW_World->topLevelItem(5); //returns 6th top level item or nullptr if no such item exists
      if (parent_item)
          new QTreeWidgetItem(parent_item, QStringList("i am a test Child!"));
      

      If you also want to select that item and make it current:

      QTreeWidgetItem* parent_item = ui->TW_World->topLevelItem(5);
      if (parent_item)
      {
          QTreeWidgetItem* child = new QTreeWidgetItem(parent_item, QStringList("i am a test Child!"));
          child->setSelected(true);
          ui->TW_World->setCurrentItem(child);
      }
      

      Btw. Out of curiosity - are you coming from Java or similar language? Your code looks kinda like that.

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Cysis145
        wrote on last edited by
        #3

        Hi Chris,

        Thank you very much for your help, it works like a charm now! :D

        I'm actually coming from an Old language called Blitz3D and i've been using c++ for just over a year now but still quite new to some aspects of QT. I'm just irritated by the fact the solution was so simple and i managed to over complicate it.

        Once again Thanks!

        1 Reply Last reply
        0

        • Login

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