1,树形结构的item设置为选中
QModelIndex rootIndex;
QModelIndex robotsIndex;
QModelIndex nonMobileIndex;
rootIndex = treeView->model()->index(0, 0);//最顶层item的index
robotsIndex = treeView->model()->index(10, 0, rootIndex);//选中item父亲的index
nonMobileIndex = treeView->model()->index(1, 0, robotsIndex);//选中item的index
treeView->setCurrentIndex(nonMobileIndex);//这种方法使用的绝对路径,但暂时没找到其他方式设置初始化时选中。
2,通过index读取建立item时setData的数据
直接通过index读取的数据是错误的一直显示0,需要获取建立QTreeView时的QStandardItemModel即treeView->setModel(itemModel)中的itemModel
QStandardItemModel * standardModel = static_cast < QStandardItemModel*>(itemModel);
QStandardItem * currentItem = standardModel->itemFromIndex(index);
QVariant data = currentItem->data();
data可转换成任意格式。
本文介绍如何在Qt中使用QTreeView控件选中特定的树形结构item,并通过index正确读取其setData时存储的数据,涉及QModelIndex、QStandardItemModel等关键类的使用。
4242

被折叠的 条评论
为什么被折叠?



