سؤال

I have a Qt application that contains a QTreeWidget. I want to replace one of the items.

I have a pointer:

QTreeWidgetItem *elementToSubstitute;

and a function that returns a QTreeWidgetItem*.

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.

How should I proceed to have the new branch replace exactly the previous one?

EDIT for example


void MainWindow::addRoot(QString name ,QString description)
{
    QTreeWidgetItem *itm = new QTreeWidgetItem(ui->tree);
    itm->setText(0,name);
    itm->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->parent(); 
    int itemIndex = parent-indexOfChid(child2); 
    parent->removeChild(child2); 
    parent->insertChild(itemIndex, child4 );

}

QTreeWidgetItem MainWindow::addChild(QTreeWidgetItem *parent,QString name ,QString description)
{
    QTreeWidgetItem *itm = new QTreeWidgetItem();
    itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled );
    itm->setText(0,name);
    itm->setText(1,description);

    addChild2(itm,"sub-one","subchild2");
    addChild2(itm,"sub-two","subchild2");
    addChild2(itm,"sub-tree","subchild2");

    parent->addChild(itm);
    return itm;

}

void MainWindow::addChild2(QTreeWidgetItem *parent,QString name ,QString description)
{
    QTreeWidgetItem *itm = new QTreeWidgetItem();

    itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);

    itm->setText(0,name);
    itm->setText(1,description);
    itm->setText(2,"test third coloumn");
    parent->addChild(itm);
}

.....

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.

هل كانت مفيدة؟

المحلول

You could try doing something like this:

QTreeWidgetItem *parent = elementToSubstitute->parent();
int itemIndex = parent->indexOfChid(elementToSubstitute);
parent->removeChild(elementToSubstitute);
parent->insertChild(itemIndex, yourNewTreeItem);

Note: if yourNewTreeItem is already in the tree, this will have no effect on its position, i.e. it will not duplicate a node.

نصائح أخرى

If your item is top-level then you could use indexOfTopLevelItem to get it's index, then remove it from tree widget, then insert replacement item using insertTopLevelItem passing index you obtained. If you item is not top-level then you could use indexOfChild and insertChild.

Why do you need to replace the whole item? Just change the contents of the current item, or copy the new item into the current one.

//assuming you have a pointer to the new and the current Item
*currentItem = *newItem; //done
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top