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.
  • HashTagJFH Offline
    HashTagJFH Offline
    HashTagJF
    wrote on 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?

    miclandM 1 Reply Last reply
    0
    • HashTagJFH HashTagJF

      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?

      miclandM Offline
      miclandM Offline
      micland
      wrote on last edited by micland
      #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
      • HashTagJFH Offline
        HashTagJFH Offline
        HashTagJF
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          miclandM 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            miclandM Offline
            miclandM Offline
            micland
            wrote on 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
            • HashTagJFH Offline
              HashTagJFH Offline
              HashTagJF
              wrote on 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
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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

                • Login

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