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



  • 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?



  • @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)");
    


  • @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.


  • Lifetime Qt Champion

    Hi,

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



  • @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...



  • 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.


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.