2

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

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

}

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

4

1 回答 1

0
QFileSystemModel* fileSystem = new QFileSystemModel();
QString myPath = fileSystem->filePath(index);
于 2019-07-21T10:55:43.597 回答