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 1 Nov 2019, 15:25 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

    O 1 Reply Last reply 1 Nov 2019, 15:28
    0
    • R russjohn834
      1 Nov 2019, 15:25

      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

      O Offline
      O Offline
      ODБOï
      wrote on 1 Nov 2019, 15:28 last edited by
      #2

      @russjohn834 hi

      see applicationDirPath

      1 Reply Last reply
      2
      • R Offline
        R Offline
        russjohn834
        wrote on 1 Nov 2019, 15:49 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)");
        
        O 1 Reply Last reply 1 Nov 2019, 16:15
        0
        • R russjohn834
          1 Nov 2019, 15:49

          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)");
          
          O Offline
          O Offline
          ODБOï
          wrote on 1 Nov 2019, 16:15 last edited by ODБOï 11 Jan 2019, 17:05
          #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 1 Nov 2019, 16:42 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!?

            J 1 Reply Last reply 1 Nov 2019, 17:53
            0
            • R russjohn834
              1 Nov 2019, 16:42

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

              J Offline
              J Offline
              JonB
              wrote on 1 Nov 2019, 17:53 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
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Nov 2019, 07:20 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 4 Nov 2019, 10:45 last edited by russjohn834 11 Apr 2019, 10:46
                  #8

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

                  1 Reply Last reply
                  2

                  4/8

                  1 Nov 2019, 16:15

                  • Login

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