Qt中QTreeView显示Json格式的文件目录结构并获取绝对路径
1.QTreeView显示Json
QTreeView显示Json参考这里。
2. 点击事件获取绝对路径
比如下图中,点击city
,路径应该是address/city
。
下面是添加点击事件,获取绝对路径:
QObject::connect(view, &QTreeView::pressed, [=](const QModelIndex &index){
QStringList absPath;
QModelIndex tmpItem = index;
while(tmpItem.isValid()) {
absPath << view->model()->itemData(tmpItem).values()[0].toString();
tmpItem = tmpItem.parent();
}
QString strPath;
for(int i = absPath.size() - 1; i >= 0 ; --i) {
strPath += absPath.at(i);
if(i != 0) {
strPath += u8"/";
}
}
qDebug() << strPath;
});
在源码中的main.cpp
中,view->show()
前面添加上面的源码可以运行试试。