PDA

View Full Version : How to print only the file name of a path



franco.amato
30th September 2008, 22:59
Hi to all,
with a QFileDialog I choose a file in my hard disk to process it.
I would to print (with a cout) only the filename instead of the whole path.

Here the code


void CoinWidget::openAudioTrack()
{
QString audioFileName = QFileDialog::getOpenFileName( "../media",
tr("Audio (*.wav *.mp3);;All Files(*)"),
this,
"open file dialog",
"Choose an audio track");

if(!audioFileName.isEmpty())
{
cout << audioFileName.latin1() << endl; //<-----this print the whole path

// retrieve the root separator
SoSeparator *lroot = model->getRoot();//about coin3d

m_sound->setAudioTrack(audioFileName.latin1());
m_sound->enableContinuousPlay(true);
m_sound->setVolume(0.5f);
m_sound->playTrack();
}
else
{
cout << "No file selected" << endl;
return;
}
}

Where I wrote "this print the whole path" I also would print the filename istead.
Any idea?
Regards

spirit
30th September 2008, 23:07
try this


...
cout << audioFileName.section('/', -1).latin1() << endl;
...

brazso
1st October 2008, 09:48
This solution is more elegant:


QFileInfo fi(audioFileName);
cout << fi.fileName().latin1() << endl;

franco.amato
2nd October 2008, 22:28
Thank you very much.
It works in both cases.

Regards,
Franco