Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to repace an element in a Qt tree widget?
    text
    copied!<p>I have a Qt application that contains a QTreeWidget. I want to replace one of the items.</p> <p>I have a pointer: </p> <pre><code>QTreeWidgetItem *elementToSubstitute; </code></pre> <p>and a function that returns a <code>QTreeWidgetItem*</code>.</p> <p>I want to overwrite the previous one with this new element, in the same place. If I delete the previous element and insert the new one after that, the new item is placed at the bottom of the tree.</p> <p>How should I proceed to have the new branch replace exactly the previous one?</p> <p><strong>EDIT for example</strong></p> <hr> <pre><code>void MainWindow::addRoot(QString name ,QString description) { QTreeWidgetItem *itm = new QTreeWidgetItem(ui-&gt;tree); itm-&gt;setText(0,name); itm-&gt;setText(1,description); QTreeWidgetItem *child1 = addChild(itm,"one","hello"); QTreeWidgetItem *child2 = addChild(itm,"two","hello"); QTreeWidgetItem *child3 = addChild(itm,"tree","hello"); QTreeWidgetItem *child4 = addChild(itm,"four","hello"); QTreeWidgetItem *parent = child2-&gt;parent(); int itemIndex = parent-indexOfChid(child2); parent-&gt;removeChild(child2); parent-&gt;insertChild(itemIndex, child4 ); } QTreeWidgetItem MainWindow::addChild(QTreeWidgetItem *parent,QString name ,QString description) { QTreeWidgetItem *itm = new QTreeWidgetItem(); itm-&gt;setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled ); itm-&gt;setText(0,name); itm-&gt;setText(1,description); addChild2(itm,"sub-one","subchild2"); addChild2(itm,"sub-two","subchild2"); addChild2(itm,"sub-tree","subchild2"); parent-&gt;addChild(itm); return itm; } void MainWindow::addChild2(QTreeWidgetItem *parent,QString name ,QString description) { QTreeWidgetItem *itm = new QTreeWidgetItem(); itm-&gt;setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable); itm-&gt;setText(0,name); itm-&gt;setText(1,description); itm-&gt;setText(2,"test third coloumn"); parent-&gt;addChild(itm); } </code></pre> <p>.....</p> <p>Now i have a pointer to one of the chiled. ( let's sa of level 1) ans i want to replace it with another. for instance i want to replace child "2" with child "4" but i want this to be put in the place f child 2.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload