我目前正在每次更新QTreeWidget时都对其进行重建。问题是我的选择被丢弃了。是否有解决方法,还是最好是找到一种更新值而不是重建树的方法?我想保留以前调用该方法时的选择。
此方法每秒调用一次。
void insertIntoTree(QTreeWidget *tree) {
tree->clear();
tree->setSelectionMode(QAbstractItemView::SingleSelection)
for (int i = 0; i < tree.members(); i++) {
tree->addTopLevelItem(parent);
parent->addChild(variousData); // Re-adds all the items into the tree, this is done every iteration
}
//Here I would like to save something that tells which Item in the widget is selected.
}
小部件的用户界面代码
<widget class="QTreeWidget" name="treeWidget">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="styleSheet">
<string notr="true">QTreeView::branch:has-siblings:!adjoins-item {
border-image: url(:/img/res/stylesheet-vline.png) 0;
}
QTreeView::branch:has-siblings:adjoins-item {
border-image: url(:/img/res/stylesheet-branch-more.png) 0;
}
QTreeView::branch:!has-children:!has-siblings:adjoins-item {
border-image: url(:/img/res/stylesheet-branch-end.png) 0;
}
QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings {
border-image: none;
image: url(:/img/res/stylesheet-branch-closed.png);
}
QTreeView::branch:open:has-children:!has-siblings,
QTreeView::branch:open:has-children:has-siblings {
border-image: none;
image: url(:/img/res/stylesheet-branch-open.png);
}</string>