【问题标题】:Get the path of selected QTreeView items获取选中的 QTreeView 项的路径
【发布时间】:2013-09-27 08:48:24
【问题描述】:

我是 Qt 的新手。我正在尝试用驱动器的所有文件和文件夹填充QTreeView。但是,我无法获取这些文件和文件夹上单击事件的文件和文件夹路径。我使用的代码如下:

void client::on_TreeView_clicked()
{
    QModelIndexList list = ui->dir_tree->selectionModel()->selectedIndexes();
    QDirModel* model = (QDirModel*)ui->dir_tree->model();
    int row = -1;
    QStringList filelist;
    foreach (QModelIndex index, list)
    {
        QFileInfo fileInfo = model->fileInfo(index);
        filelist << fileInfo.filePath();
    }
}

我在此声明中收到错误:

filelist << fileInfo.filePath();

错误信息是这样的:

下级停止了,因为它收到了操作员的信号 系统。信号名称:EXC_BAD_ACCESS 信号含义:无法访问 记忆。我认为它在某个地方存在内存问题。

我的 mainwindow.cpp 看起来像这样:

#include "mainwindow.h"
#include "ui_mainwindow.h"


 QStringList templist;
 QString filename;
 QString path;
 QString username;
 QStandardItemModel *standardModel;
 QSortFilterProxyModel *proxyModel;
 MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
ui->setupUi(this);
standardModel= new QStandardItemModel;
QStandardItem *rootnode= standardModel->invisibleRootItem();
QStandardItem *DriveItem = new QStandardItem("Devices");
rootnode->appendRow(DriveItem);
QIcon disk(":/Images/disk.png");
for(int i=0;i<driveslist.count();i++)
{
    QStandardItem *treeItem1 = new QStandardItem(driveslist[i]);
    treeItem1->setIcon(disk);
    DriveItem->appendRow(treeItem1);
}
QStandardItem *PlacesItem = new QStandardItem("Places");
rootnode->appendRow(PlacesItem);
QStandardItem *placeItem1 = new QStandardItem("Desktop");
QStandardItem *placeItem2 = new QStandardItem("Root");
QStandardItem *placeItem3 = new QStandardItem("Applications");
QStandardItem *placeItem4 = new QStandardItem("Documents");
PlacesItem->appendRow(placeItem1);
QIcon disk1(":/Images/t 1.png");
placeItem1->setIcon(disk1);
PlacesItem->appendRow(placeItem2);
QIcon disk2(":/Images/t 2.png");
placeItem2->setIcon(disk2);
PlacesItem->appendRow(placeItem3);
QIcon disk3(":/Images/t 3.png");
placeItem3->setIcon(disk3);
PlacesItem->appendRow(placeItem4);
QIcon disk4(":/Images/t 4.png");
placeItem4->setIcon(disk4);
ui->treeView->setModel(standardModel);
ui->treeView->expandAll();
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_treeView_clicked(QModelIndex index)
{
  On click of this tree items,  treeView_2 will be generated and on click of those items      on_treeView_2_clicked(QModelIndex index) will be called.
}

void MainWindow::on_treeView_2_clicked(QModelIndex index)
{
    QModelIndexList CurrentIndex = ui->treeView_2->selectionModel()->selectedRows();
    QFileSystemModel *model=(QFileSystemModel*)ui->treeView_2->model();
    QList <QString >SelectedPathList;
    if(CurrentIndex.size())
    {
        for(long long ll = 0;ll < CurrentIndex.size(); ll++)
    {
        QModelIndex Index = CurrentIndex[ll];
        QString m_strSourcePath = model->filePath(Index);
        SelectedPathList << m_strSourcePath;
    }
}

}

谁能帮我解决这个问题?谢谢。

【问题讨论】:

  • 你为什么不简单地打电话给model-&gt;filePath(index);
  • 是的,我已经尝试过了。但它也显示相同的错误。
  • 错误信息是什么?请在此处粘贴。
  • 下级停止了,因为它收到了来自操作系统的信号。信号名称:EXC_BAD_ACCESS 信号含义:无法访问内存。我认为它在某个地方存在内存问题。
  • 你能粘贴一个简单的代码来重现这个问题,我们可以构建测试吗?也就是说,包含基于 ssccce.org 描述的 main.cpp 等。

标签: qt qtreeview


【解决方案1】:
QFileSystemModel* fileSystem = new QFileSystemModel();
QString myPath = fileSystem->filePath(index);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2016-06-04
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2019-12-16
    相关资源
    最近更新 更多