如何替换 Qt 树小部件中的元素?

发布于 2024-12-08 03:23:28 字数 1969 浏览 0 评论 0原文

我有一个包含 QTreeWidget 的 Qt 应用程序。 我想更换其中一件物品。

我有一个指针:

QTreeWidgetItem *elementToSubstitute;

和一个返回 QTreeWidgetItem* 的函数。

我想在同一位置用这个新元素覆盖前一个元素。 如果我删除前一个元素并在其后插入新元素,则新项目将放置在树的底部。

我应该如何让新分支完全取代以前的分支?

例如编辑


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);
}

.....

现在我有一个指向其中一个的指针。 (让我们sa级别1)我想用另一个替换它。例如,我想用子“4”替换子“2”,但我希望将其放在子 2 的位置。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-12-15 03:23:28

您可以尝试执行以下操作:

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

注意:如果 yourNewTreeItem 已在树中,则这不会对其位置产生影响,即不会复制节点。

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.

鲜肉鲜肉永远不皱 2024-12-15 03:23:28

如果您的项目是顶级的,那么您可以使用indexOfTopLevelItem来获取它的索引,然后将其从树小部件中删除,然后使用insertTopLevelItem传递您获得的索引插入替换项目。如果您的项目不是顶级,那么您可以使用indexOfChild 和insertChild。

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.

赠我空喜 2024-12-15 03:23:28

为什么需要更换整个项目?只需更改当前项目的内容,或将新项目复制到当前项目中即可。

//assuming you have a pointer to the new and the current Item
*currentItem = *newItem; //done

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文