tags:

views:

81

answers:

2

Greetings all,

eg:

QFile f("/home/umanga/Desktop/image.jpg");

How I get only the filename - "image.jpg"

Thanks in advance

+7  A: 

Use a QFileInfo to strip out the path (if any):

QFileInfo fileInfo(f.fileName());
QString filename(fileInfo.fileName());
Arnold Spence
+1. A bit more succinct than mine :-)
paxdiablo
Yeah, Qt has tricks hidden all over the place. I'm still discovering them :)
Arnold Spence
You can make this even shorter by giving the QFile as a parameter to the QFileInfo constructor: `QFileInfo fileInfo(f);`
Roku
Good one! I missed that constructor option.
Arnold Spence
+2  A: 

One approach, not necessarily the best: from a QFile, you can get the file specification with QFile::fileName():

QFile f("/home/umanga/Desktop/image.jpg");
QString str = f.fileName();

then you can just use the string features like QString::split:

QStringList parts = str.split("/");
QSTring lastBit = parts.at(parts.size()-1);
paxdiablo
QString section is more situable:
Kamil Klimek
QString lastBit = str.section("/", -1, -1);
Kamil Klimek
@Kamil, you should leave that as an answer rather than a comment, then I'll upvote it :-)
paxdiablo