我一直在处理一个本地目录的简单QTreeView。目标是允许用户浏览到他/她的目录并选择正确的csv文件。
我已经创建了一个QFileSystemModel并用一个QTreeView显示了它。我很困惑如何从当前选定的节点获取文件名。
我阅读了文档,发现了以下信号/插槽配对:
connect(tree, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTreeWidgetEvent(QModelIndex)));
但是我不知道一旦激活了QModelIndex该怎么办。我知道您应该用这个索引对QTreeView进行索引,但我不确定如何进行索引。
任何帮助都是非常感谢的。
编辑:添加代码,这样人们就可以看到我在做什么。
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath("/");
tree = new QTreeView;
tree->setModel(model);
tree->setRootIndex(model->index("/home/Missions/"));
tree->setColumnWidth(0, 350);
connect(tree, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTreeWidgetEvent(QModelIndex)));发布于 2013-09-21 20:26:31
WhatEverClassInheritingQObject::handleTreeWidgetEvent(const QModelIndex& index)
{
const QString valuablePathAskedFor(fileSystemModel->fileName(index));
...
}发布于 2013-09-21 18:07:37
您可以在仅基于QString的setData方法filePath()方法中检索路径,因此每次用户检查(或未选中)模型中的某个目录或文件时,都会调用路径,然后需要将所有这些路径存储在某个conatainer中,并实现方法来返回:
bool MyQFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.column() != 0 || role != Qt::CheckStateRole)
return QFileSystemModel::setData(index, value, role);
int newCheckState = value.toInt();
QString filePath = filePath(index);
if (newCheckState == Qt::Checked || newCheckState == Qt::PartiallyChecked )
checkedPaths.insert(filePath);
else
checkedPaths.remove(filePath);
emit dataChanged(index, index.child(index.row(),0));
return true;
}
class MyQFileSystemModel : public QFileSystemModel
{
Q_OBJECT
public:
//...
QSet<QString> getChecked() const { return checkedPaths; }
private:
QSet<QString> chackedPaths;
//...
};https://stackoverflow.com/questions/18935701
复制相似问题