Results 1 to 4 of 4

Thread: How to print only the file name of a path

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    620
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to print only the file name of a path

    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

    Qt Code:
    1. void CoinWidget::openAudioTrack()
    2. {
    3. QString audioFileName = QFileDialog::getOpenFileName( "../media",
    4. tr("Audio (*.wav *.mp3);;All Files(*)"),
    5. this,
    6. "open file dialog",
    7. "Choose an audio track");
    8.  
    9. if(!audioFileName.isEmpty())
    10. {
    11. cout << audioFileName.latin1() << endl; //<-----this print the whole path
    12.  
    13. // retrieve the root separator
    14. SoSeparator *lroot = model->getRoot();//about coin3d
    15.  
    16. m_sound->setAudioTrack(audioFileName.latin1());
    17. m_sound->enableContinuousPlay(true);
    18. m_sound->setVolume(0.5f);
    19. m_sound->playTrack();
    20. }
    21. else
    22. {
    23. cout << "No file selected" << endl;
    24. return;
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to print only the file name of a path

    try this
    Qt Code:
    1. ...
    2. cout << audioFileName.section('/', -1).latin1() << endl;
    3. ...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2008
    Location
    Budapest, Hungary
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to print only the file name of a path

    This solution is more elegant:

    Qt Code:
    1. QFileInfo fi(audioFileName);
    2. cout << fi.fileName().latin1() << endl;
    To copy to clipboard, switch view to plain text mode 
    Last edited by brazso; 1st October 2008 at 08:51. Reason: missing [code] tags

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    620
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to print only the file name of a path

    Thank you very much.
    It works in both cases.

    Regards,
    Franco
    Franco Amato

Similar Threads

  1. How to Print a doc file (or binary file) to printer
    By rmagro in forum Qt Programming
    Replies: 15
    Last Post: 5th September 2008, 15:46
  2. how to print an html file?
    By patcito in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2008, 16:50
  3. Problem to find file path
    By phillip_Qt in forum Qt Programming
    Replies: 14
    Last Post: 28th April 2008, 10:06
  4. Problems with korean symbols in file path
    By Raistlin in forum Qt Programming
    Replies: 5
    Last Post: 6th April 2008, 13:59
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.