Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Relative path to save file



  • Hi All,

    I need to save an xml. I'm getting the file name as:

    QString filename = QFileDialog::getSaveFileName(this, "Save Xml", ui->lineEdit_3->text(), "Xml files (*.xml)");
    

    File need to saved here in the location (build folder of the project):
    C:\Users\L23454\Code_Refer\DataManagement\build-SQLDataManagement-Desktop_Qt_5_12_0_MSVC2015_64bit-Debug\Patient Data

    And, my project is here:
    C:\Users\L23454\Code_Refer\DataManagement

    How do I mention the path where the file needs to be saved?
    Thanks in advance





  • Thank you @LeLev
    A few doubts: i need to save data one step back from: QCoreApplication::applicationDirPath() so I did this:

      QDir dir(QCoreApplication::applicationDirPath());
      QString location = dir.relativeFilePath("./Patient Data/");
    
    

    Is that correct?

    Then how do I mention this location here with the file name:

    QString filename = QFileDialog::getSaveFileName(this, "Save Xml", ui->lineEdit_3->text(), "Xml files (*.xml)");
    


  • https://doc.qt.io/qt-5/qdir.html#relativeFilePath

    @russjohn834 said in Relative path to save file:

    Is that correct?

    you can check it

        QDir dir(QCoreApplication::applicationDirPath());
    
        QString location = dir.relativeFilePath("../PatientData/");
         qDebug()<<location;
        QFile file(location + "/test.txt");
        QTextStream out(&file);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
            return;
        }
        out << "test" ;
        file.close();    
    


  • @LeLev . Thank you.

    I need to save file to a location, say for ex. (QCoreApplication::applicationDirPath()) with a file name (ui->lineEdit_3->text()).

    The file should be saved to the location upon clicking the button.

    But, I dont know why if I do this, the file file dialogue window popping in

     QString filename = QFileDialog::getSaveFileName(this, "Save Xml", location.append("/"+ui->lineEdit_3->text()), "Xml files (*.xml)");
        QFile file(filename);
    

    Any thoughts!?



  • @russjohn834
    You call QFileDialog::getSaveFileName() in order to present the user with the file dialog to choose where to save, with your location as the default. If you already know what path you want to save to, like in your case, you don't want to call this, just save to the file without asking the user further.


  • Lifetime Qt Champion

    Hi
    You just need to open a file and save the data.
    Something like:

    QString filename =ui->lineEdit_3->text();
    QString path=QCoreApplication::applicationDirPath()+"/"+filename +".xml";
    QFile file(path);
    QTextStream out(&file);
    // try to open
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
    return;
    }
    // save the sdata
    out << "test" ;
    file.close();



  • thanks a lot, @LeLev , @JonB and @mrjj


Log in to reply