繁体   English   中英

使用QFileSystemModel访问文件路径

[英]Getting access to filepath with QFileSystemModel

我在QFileSystemModel中有一个QListView ,它使我可以选择文件系统中的项。

选择项目时,我想在QMessageBox返回文件路径。

到目前为止,我有以下代码:

filemodel = new QFileSystemModel(this);
filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
filemodel->setNameFilters(filters);
filemodel->setNameFilterDisables(false);
filemodel->setRootPath(sPath);

//get file path
QString filepath = filemodel->fileName(index);
QMessageBox::information(this, "title", filepath);

ui->listView->setModel(filemodel);

这将创建文件模型。

我收到此错误:

mainwindow.cpp:46:错误:没有匹配的函数调用'QFileSystemModel :: fileName(char *(&)(const char *,int))'

这是解决这个问题的正确方法吗? 选择一个项目时返回文件路径?

编辑@ dunc123

在构造函数中:

connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectedFile(QItemSelection one, QItemSelection two)));

selectedFile:

void MainWindow::selectedFile(QItemSelection one, QItemSelection two){
    QString file_name = filemodel->fileName(ui->listView->selectionModel()->currentIndex());
    QMessageBox::information(this, "title", file_name);
}

它会生成并运行,但是当我单击文件时,出现以下错误:

Object::connect: No such slot MainWindow::selectedFile(QItemSelection one, QItemSelection two) in ../Images/mainwindow.cpp:26

Object :: connect:(接收者名称:“ MainWindow”)

我假设我传递变量的方式是错误的?

你能帮我吗?

您需要将QModelIndex对象传递给QFileSystemModel的fileName方法。 似乎符号“索引”正在作为函数解析。 猜测您的类中有一个名为index的成员函数。

编辑:这里更大的问题是,当您在QListView中选择一个项目时,您希望某些事情发生,但是您要在构造函数中放入处理此代码的代码。 您需要在班级中创建一个插槽,并将其连接到选定项目时发出的信号。

connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &), this, SLOT(...));

在此插槽中,应调用fileName方法并显示该信息。 您还需要使filemodel成为类的成员变量,以便您可以从插槽中访问它。

编辑2:调用connect时指定插槽的方式不正确,应为:

connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectedFile(QItemSelection , QItemSelection)));

但是,由于您没有在插槽中使用任何一个参数,因此实际上您可以将所有参数从插槽中一起删除,例如,在标头中将其定义为:

void selectedFile();

并使用以下命令进行连接:

connect(ui->listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectedFile()));

QT将计算出您不需要信号中的任何一个参数。

您可以使用QItemSelectionModel :: selectedIndexes()函数获取所选索引的列表。

以下是如何使用它的示例:

QModelIndexList list = ui->listView->selectionModel()->selectedIndexes();
foreach (QModelIndex index, list)
{
    QString file_name = fileModel->fileName(index);            
}

或者,如果您只能选择一个项目,则可以使用QItemSelectionModel :: currentIndex函数,如下所示:

QString file_name = fileMode->fileName(ui->listView->selectionModel()->currentIndex());

您也可以将QItemSelectionModel :: selectionChanged信号连接到插槽,并以类似的方式使用它。 QListView具有selectionModel()函数,可用于检索QItemSelectionModel对象。 QItemSelection具有一个index()函数,该函数返回QModelIndexList

QString filepath = filemodel->fileName(index);

mainwindow.cpp:46: error: no matching function for call to 'QFileSystemModel::fileName(char* (&)(const char*, int))'

看起来“索引”是一个函数,而不是QModelIndex ...

您可以使用连接到模型信号“ currentChanged(QModelIndex,QModelIndex)”的插槽来获取新的选定索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM