sark9012
sark9012

Reputation: 5717

Getting access to filepath with QFileSystemModel

I have a QFileSystemModel within a QListView that allows me to select items within the file system.

When I select an item, I want to return the filepath in a 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))'

Is this the correct way to go about this? Returning the filepath when an item is selected?

EDIT @dunc123

In constructor:

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

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

I'm assuming the way I pass the variables is wrong?

Could you help me out?

Upvotes: 1

Views: 2685

Answers (3)

thuga
thuga

Reputation: 12901

You can get a list of selected indexes with QItemSelectionModel::selectedIndexes() function.

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:

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. QListView has a selectionModel() function that you can use to retrieve a QItemSelectionModel object. QItemSelection has a indexes() function that returns a QModelIndexList.

Upvotes: 1

dunc123
dunc123

Reputation: 2713

You need to pass a QModelIndex object to the fileName method of QFileSystemModel. It appears the symbol "index" is being resolved as a function. At a guess you have a member function named index in your class.

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. 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. You'll need to make filemodel a member variable of your class as well so that you can access it from the slot.

Edit 2: The way you are specifying your slot when calling connect is incorrect, it should be:

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, e.g. 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.

Upvotes: 1

Sylvain V
Sylvain V

Reputation: 194

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...

You can use a slot that is connected to the model's signal "currentChanged(QModelIndex,QModelIndex)" to get the new selected index.

Upvotes: 0

Related Questions