PDA

View Full Version : Extracting path of a file sleected from a listbox



sargeslash
7th January 2013, 15:36
Hi

I am trying to get the path of a selected file in a listbox.I am able to get the filename after selecting the file by the following code

QModelIndex index = ui->treeView->currentIndex();
ui->listWidget_2->addItem(index.data().toString());
I want to get the full path of the selected file also, if there is any way please guide me.

thanks

ChrisW67
7th January 2013, 20:07
We have no idea what is in the model you have, how you populated it, or what non-full file name you have so we cannot possibly give you concrete direction.

sargeslash
8th January 2013, 10:51
Hi
I am sorry for so less information, I have used the following code

QFileSystemModel *model = new QFileSystemModel;
model->setRootPath("/home/");
ui->treeView->setModel(model);
ui->treeView->setRootIndex(model->index("/home/"));

So I have this file browser in my treeview and I am able to extract file names from this treeview to a listbox.
I also want to have full path of the selected file from the tree view.

Santosh Reddy
8th January 2013, 10:58
Take a look at QFileInfo, you could use following


QString QFileInfo::fileName () const
QString QFileInfo::baseName () const;
QString QFileInfo::completeBaseName () const

sargeslash
8th January 2013, 12:44
Can I use the FileInfo for the selected index.data() from the treeView, since its not a file?

Santosh Reddy
8th January 2013, 13:07
Can I use the FileInfo for the selected index.data() from the treeView, since its not a file?
Yes you can. QFileInfo can be used with the file name (QString()) which can be index.data().

ChrisW67
8th January 2013, 21:40
QAbstractItemView::currentIndex() in combination with QFileSystemModel::filePath() seems like the obvious option.

sargeslash
9th January 2013, 10:35
QFileSystemModel::filePath()
This worked for me, thank u all for the help