简体   繁体   English

使用QFileSystemModel访问文件路径

[英]Getting access to filepath with QFileSystemModel

I have a QFileSystemModel within a QListView that allows me to select items within the file system. 我在QFileSystemModel中有一个QListView ,它使我可以选择文件系统中的项。

When I select an item, I want to return the filepath in a QMessageBox . 选择项目时,我想在QMessageBox返回文件路径。

So far I have the following code: 到目前为止,我有以下代码:

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

This creates the filemodel. 这将创建文件模型。

I'm getting this error: 我收到此错误:

mainwindow.cpp:46: error: no matching function for call to 'QFileSystemModel::fileName(char* (&)(const char*, int))' mainwindow.cpp:46:错误:没有匹配的函数调用'QFileSystemModel :: fileName(char *(&)(const char *,int))'

Is this the correct way to go about this? 这是解决这个问题的正确方法吗? Returning the filepath when an item is selected? 选择一个项目时返回文件路径?

EDIT @dunc123 编辑@ dunc123

In constructor: 在构造函数中:

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

selectedFile: selectedFile:

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

It builds and runs but when I click on a file, I get the following error: 它会生成并运行,但是当我单击文件时,出现以下错误:

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

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

I'm assuming the way I pass the variables is wrong? 我假设我传递变量的方式是错误的?

Could you help me out? 你能帮我吗?

You need to pass a QModelIndex object to the fileName method of QFileSystemModel. 您需要将QModelIndex对象传递给QFileSystemModel的fileName方法。 It appears the symbol "index" is being resolved as a function. 似乎符号“索引”正在作为函数解析。 At a guess you have a member function named index in your class. 猜测您的类中有一个名为index的成员函数。

Edit: The larger issue here is that you want something to happen when you select an item in your QListView, but you are putting the code to handle this in the constructor. 编辑:这里更大的问题是,当您在QListView中选择一个项目时,您希望某些事情发生,但是您要在构造函数中放入处理此代码的代码。 You need to create a slot in your class and connect this to the signal emitted when an item is selected. 您需要在班级中创建一个插槽,并将其连接到选定项目时发出的信号。

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

In this slot you should call the fileName method and display that information. 在此插槽中,应调用fileName方法并显示该信息。 You'll need to make filemodel a member variable of your class as well so that you can access it from the slot. 您还需要使filemodel成为类的成员变量,以便您可以从插槽中访问它。

Edit 2: The way you are specifying your slot when calling connect is incorrect, it should be: 编辑2:调用connect时指定插槽的方式不正确,应为:

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

However, since you aren't using either of those parameters in your slot, you can infact remove them all together from your slot, eg define it in your header as: 但是,由于您没有在插槽中使用任何一个参数,因此实际上您可以将所有参数从插槽中一起删除,例如,在标头中将其定义为:

void selectedFile();

And connect it using: 并使用以下命令进行连接:

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

QT will work out that you don't want either of the parameters from the signal. QT将计算出您不需要信号中的任何一个参数。

You can get a list of selected indexes with QItemSelectionModel::selectedIndexes() function. 您可以使用QItemSelectionModel :: selectedIndexes()函数获取所选索引的列表。

Heres an example of how you can use it: 以下是如何使用它的示例:

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

Or if you can only select a single item, you can use the QItemSelectionModel::currentIndex function like this: 或者,如果您只能选择一个项目,则可以使用QItemSelectionModel :: currentIndex函数,如下所示:

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

You can also connect the QItemSelectionModel::selectionChanged signal to a slot and use that in similar fashion. 您也可以将QItemSelectionModel :: selectionChanged信号连接到插槽,并以类似的方式使用它。 QListView has a selectionModel() function that you can use to retrieve a QItemSelectionModel object. QListView具有selectionModel()函数,可用于检索QItemSelectionModel对象。 QItemSelection has a indexes() function that returns a QModelIndexList . QItemSelection具有一个index()函数,该函数返回QModelIndexList

QString filepath = filemodel->fileName(index);

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

Looks like "index" is a function, not a QModelIndex... 看起来“索引”是一个函数,而不是QModelIndex ...

You can use a slot that is connected to the model's signal "currentChanged(QModelIndex,QModelIndex)" to get the new selected index. 您可以使用连接到模型信号“ currentChanged(QModelIndex,QModelIndex)”的插槽来获取新的选定索引。

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

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