Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the base name from full file path
QtWS25 Last Chance

How to get the base name from full file path

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ayush Gupta
    wrote on 22 May 2019, 06:42 last edited by
    #1

    I am using below code

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
    QDir::currentPath(),
    tr("txt (*.txt)"));

    it gives full path like C:\test\1.txt

    I need to get only 1.txt how can I get it?

    Thanks

    R J 2 Replies Last reply 22 May 2019, 06:54
    0
    • A Ayush Gupta
      22 May 2019, 06:42

      I am using below code

      QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
      QDir::currentPath(),
      tr("txt (*.txt)"));

      it gives full path like C:\test\1.txt

      I need to get only 1.txt how can I get it?

      Thanks

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 22 May 2019, 06:54 last edited by
      #2

      @Ayush-Gupta
      QFileInfo::fileName()

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      5
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 22 May 2019, 06:55 last edited by sierdzio
        #3
        const QFileInfo info(fileName);
        const QString file(info.fileName());
        

        (Z(:^

        1 Reply Last reply
        5
        • A Ayush Gupta
          22 May 2019, 06:42

          I am using below code

          QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
          QDir::currentPath(),
          tr("txt (*.txt)"));

          it gives full path like C:\test\1.txt

          I need to get only 1.txt how can I get it?

          Thanks

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 22 May 2019, 07:01 last edited by J.Hilk
          #4

          @Ayush-Gupta

          to add a 3rd way.

          QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath(), tr("txt (*.txt)"));
          
          fileName = QString(fileName.splitRef("/").last());
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2

          1/4

          22 May 2019, 06:42

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved