我想在treeview中选择一些不同的文件夹。QT中有两个解决方案,如下所示:

不管怎样,谢谢你。
发布于 2014-10-01 13:04:21
应该对你的例子进行一些调整,以使用QFileSystemModel。
诀窍是将checkedIndexes集声明为mutable,并在CFileSystemModel::data方法中更新它。
QVariant CFileSystemModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::CheckStateRole)
{
if (checkedIndexes.contains(index))
{
return checkedIndexes.contains(index) ? Qt::Checked : Qt::Unchecked;
}
else
{
int checked = Qt::Unchecked;
QModelIndex parent = index.parent();
while (parent.isValid())
{
if (checkedIndexes.contains(parent))
{
checked = Qt::Checked;
break;
}
parent = parent.parent();
}
if (checked == Qt::Checked)
{
checkedIndexes.insert(parent);
}
return checked;
}
}
else
{
return QFileSystemModel::data(index, role);
}
}在视图中打开目录节点时,QFileSystemModel将开始加载新内容。加载后,视图使用CFileSystemModel::data函数替换新数据,该函数检查是否检查了新节点锚点并返回正确的Qt::CheckStateRole值(并更新checkedIndexes集)。
https://stackoverflow.com/questions/26125363
复制相似问题