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

Relative path to save file

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.6k Views 1 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.
  • R Offline
    R Offline
    russjohn834
    wrote on last edited by
    #1

    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

    ODБOïO 1 Reply Last reply
    0
    • R russjohn834

      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

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @russjohn834 hi

      see applicationDirPath

      1 Reply Last reply
      2
      • R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #3

        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)");
        
        ODБOïO 1 Reply Last reply
        0
        • R russjohn834

          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)");
          
          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          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();    
          
          1 Reply Last reply
          4
          • R Offline
            R Offline
            russjohn834
            wrote on last edited by
            #5

            @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!?

            JonBJ 1 Reply Last reply
            0
            • R russjohn834

              @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!?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              1 Reply Last reply
              4
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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();

                1 Reply Last reply
                3
                • R Offline
                  R Offline
                  russjohn834
                  wrote on last edited by russjohn834
                  #8

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

                  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