首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从QTreeView返回节点名

问如何从QTreeView返回节点名
EN

Stack Overflow用户
提问于 2013-09-21 17:50:27
回答 2查看 351关注 0票数 1

我一直在处理一个本地目录的简单QTreeView。目标是允许用户浏览到他/她的目录并选择正确的csv文件。

我已经创建了一个QFileSystemModel并用一个QTreeView显示了它。我很困惑如何从当前选定的节点获取文件名。

我阅读了文档,发现了以下信号/插槽配对:

connect(tree, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTreeWidgetEvent(QModelIndex)));

但是我不知道一旦激活了QModelIndex该怎么办。我知道您应该用这个索引对QTreeView进行索引,但我不确定如何进行索引。

任何帮助都是非常感谢的。

编辑:添加代码,这样人们就可以看到我在做什么。

代码语言:javascript
复制
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)));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-21 20:26:31

代码语言:javascript
复制
WhatEverClassInheritingQObject::handleTreeWidgetEvent(const QModelIndex& index)
{
   const QString valuablePathAskedFor(fileSystemModel->fileName(index));
   ...
}
票数 0
EN

Stack Overflow用户

发布于 2013-09-21 18:07:37

您可以在仅基于QString的setData方法filePath()方法中检索路径,因此每次用户检查(或未选中)模型中的某个目录或文件时,都会调用路径,然后需要将所有这些路径存储在某个conatainer中,并实现方法来返回:

代码语言:javascript
复制
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;
//...
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18935701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档