获取所选QTreeView项的路径

时间:2013-09-27 08:48:25

标签: qt qtreeview

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

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;
    }
}

}

任何人都可以帮我这个吗?感谢。

1 个答案:

答案 0 :(得分:0)

QFileSystemModel* fileSystem = new QFileSystemModel();
QString myPath = fileSystem->filePath(index);