Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. The path after saving a file is not the default path when saving again in the QFileDialog.

The path after saving a file is not the default path when saving again in the QFileDialog.

Scheduled Pinned Locked Moved Solved Brainstorm
7 Posts 3 Posters 3.5k 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.
  • H Offline
    H Offline
    HashTagJF
    wrote on 2 Aug 2016, 03:01 last edited by
    #1

    I have the following codes in my QFileDialog for windows. What I want to do is when I save my "sample.txt" file. I want to save the next file to the path where I saved the previous file.

    QString file = QFileDialog::getSaveFileName(this,tr("Notepad- Save file"),"Untitled.txt",
    tr("Text Files (*txt)");

    ****When I write my codes like this.
    --> QString file = QFileDialog::getSaveFileName(this,tr("Notepad- Save file")," ",
    tr("Text Files (*txt)");

    ***removing the "Untitled.txt" would work but the title for the filename will disappear. I want my QFileDialog to be perfect as the filedialog in the Microsoft word. Where the filename field has a temporary filename and the file would save to the path where I save the previous projects. How am I suppose to do that?

    M 1 Reply Last reply 2 Aug 2016, 06:27
    0
    • H HashTagJF
      2 Aug 2016, 03:01

      I have the following codes in my QFileDialog for windows. What I want to do is when I save my "sample.txt" file. I want to save the next file to the path where I saved the previous file.

      QString file = QFileDialog::getSaveFileName(this,tr("Notepad- Save file"),"Untitled.txt",
      tr("Text Files (*txt)");

      ****When I write my codes like this.
      --> QString file = QFileDialog::getSaveFileName(this,tr("Notepad- Save file")," ",
      tr("Text Files (*txt)");

      ***removing the "Untitled.txt" would work but the title for the filename will disappear. I want my QFileDialog to be perfect as the filedialog in the Microsoft word. Where the filename field has a temporary filename and the file would save to the path where I save the previous projects. How am I suppose to do that?

      M Offline
      M Offline
      micland
      wrote on 2 Aug 2016, 06:27 last edited by micland 8 Feb 2016, 06:31
      #2

      @HashTagJF
      Opening the QFileDialog using the static method QFileDialog::getSaveFileName(...) is stateless, so the dialog does not remember the last used directory. You have to store it separately and pass it as third argument (you can specify a complete path, not just a filename).

      QString lastFile = ....; // e.g. /home/xyz/Documents/myFile.txt
      QString newFile = QDir(lastFile).absolutePath().filePath(tr("Untitled.txt")); // set a temporary name
      QString file = QFileDialog::getSaveFileName(this,tr("Notepad- Save file"), newFile, tr("Text Files (*txt)");
      
      1 Reply Last reply
      0
      • H Offline
        H Offline
        HashTagJF
        wrote on 2 Aug 2016, 06:48 last edited by
        #3

        @micland said:
        good day thank you so much for your help but I am having an error with this part...

        QString newFile = QDir(lastFile).absolutePath().filePath(tr("Untitled.txt")); // set a temporary name

        QT says that Class QString has no member named filepath.

        I do appreciate your help but I hope you can help me with this one too. Thank you so much.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Aug 2016, 07:06 last edited by
          #4

          Hi,

          That's because absolutePath returns a QString. You're likely looking for QDir::absoluteFilePath

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply 2 Aug 2016, 07:47
          0
          • S SGaist
            2 Aug 2016, 07:06

            Hi,

            That's because absolutePath returns a QString. You're likely looking for QDir::absoluteFilePath

            M Offline
            M Offline
            micland
            wrote on 2 Aug 2016, 07:47 last edited by
            #5

            @SGaist said:

            Hi,

            That's because absolutePath returns a QString. You're likely looking for QDir::absoluteFilePath

            Ouch, right. I did not try it, I was just typing the demo code before having the first cup of coffee ;-)
            But following this approach is the right way...

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HashTagJF
              wrote on 2 Aug 2016, 08:44 last edited by
              #6

              Thank you guys for help. I'm trying out the codes but still got some errors and still trying to figure out the solution. But thanks guys.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 2 Aug 2016, 10:40 last edited by
                #7

                You're welcome !

                Since you have that part working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                1/7

                2 Aug 2016, 03:01

                • Login

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