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. InsertChild does not its work (solved)
Qt 6.11 is out! See what's new in the release blog

InsertChild does not its work (solved)

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.2k 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.
  • V Offline
    V Offline
    valeSimu
    wrote on last edited by
    #1

    I have a QTreeWidget and I would like to insert a child at a certain index. I tried with

    void QTreeWidgetItem::insertChild ( int index, QTreeWidgetItem * child )

    but any index I put, it inserts the child at the end of all children the parent has.

    Anyone can help me please? Thanks!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      valeSimu
      wrote on last edited by
      #2

      I found the solution:

      My first code was this

      @
      QTreeWidgetItem* newVtxItem = new QTreeWidgetItem(parentItem);
      newVtxItem->setText(0, "vtx 1");
      newVtxItem->setText(1, "-1");
      childOfNameProfile->insertChild(0,newVtxItem);@

      Then I correct it by deleting the parentItem of the new QTreeWidgetItem in this way:

      @
      QTreeWidgetItem* newVtxItem = new QTreeWidgetItem();
      newVtxItem->setText(0, "vtx 1");
      newVtxItem->setText(1, "-1");
      childOfNameProfile->insertChild(0,newVtxItem);@

      The reason why the child was inserted after all children is because when you create a QTreeWidgetItem and defining a parent, Qt put it directly at the end of the children list of the parent. So insertChild, as the documentation said:

      bq. If the child has already been inserted somewhere else it won't be inserted again.

      I hope I could help someone. Enjoy Qt!

      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