如何替换 Qt 树小部件中的元素?
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试执行以下操作:
注意:如果
yourNewTreeItem
已在树中,则这不会对其位置产生影响,即不会复制节点。You could try doing something like this:
Note: if
yourNewTreeItem
is already in the tree, this will have no effect on its position, i.e. it will not duplicate a node.如果您的项目是顶级的,那么您可以使用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.
为什么需要更换整个项目?只需更改当前项目的内容,或将新项目复制到当前项目中即可。
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.