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
Forum Updated to NodeBB v4.3 + New Features

How to get the base name from full file path

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 6.0k Views 2 Watching
  • 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 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

    raven-worxR J.HilkJ 2 Replies Last reply
    0
    • A Ayush Gupta

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by sierdzio
        #3
        const QFileInfo info(fileName);
        const QString file(info.fileName());
        

        (Z(:^

        1 Reply Last reply
        5
        • A Ayush Gupta

          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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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

          • Login

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