1、qtreewidget初始化、槽函数selectIndex
QMap<QTreeWidgetItem *, int> listTreeItem; //因为treeWidget没有selectIndex,故做的映射
QStringList headers;
headers << tr("用户组列表");
ui->treeWidget->setColumnCount(1);
ui->treeWidget->setHeaderLabels(headers);
ui->treeWidget->blockSignals(true);
//加入树列表
while (ui->treeWidget->topLevelItemCount()) {
QTreeWidgetItem* item = ui->treeWidget->takeTopLevelItem(0);
delete item;
}
for (size_t i = 0; i < mGroupList.size(); i++)
{
QStringList treeRoot;
treeRoot << mGroupList[i];
QTreeWidgetItem *root = new QTreeWidgetItem(ui->treeWidget, treeRoot);
ui->treeWidget->addTopLevelItem(root);
if (i == 0) {//选中第一个
root->setSelected(true);
}
listTreeItem.insert(root, i);
}
ui->treeWidget->blockSignals(false);
//槽函数
void manageGroup::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
selectIndex= listTreeItem[item];
}
2、 qtreewidget遍历,查找某一项,设置当前
QString m_tmp = "_name";
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it) {
if (m_tmp == (*it)->text(0/*int column*/)) {
break;
}
it++;
}
ui->treeWidget->setFocus();
ui->treeWidget->setCurrentItem(*it);