Search code examples
c++qtqtexteditqfiledialog

How to save a text file directly without using QfileDialog box?


enter image description here

This is my sample UI, the white box is a textbox which will have some items, my main question is that when i click "Save/Refresh" qpushbutton, i want to save all of the qtextbox text into a textfile/sample_name.xml into a designated folder, but i dont wanna go through Qfiledialog box and having to decide/browse a location in which the file needs to be saved, i just want it to be saved at a fixed place in C-drive ,

and also the text in the qtextbox should again be loaded with that sample_name.xml file, i know the content is gonna be the same as i just saved it , but still i need it for some other functionality.

How can i acheive this without the involvement of qfiledialog ?


Solution

  • You will have to provide a static path within the function that listens at your Save button. Your listener function would be of a similar format:

    void save(){
      //assuming content of textbox has been stored in variable 'content'
      ofstream myfile;
      myfile.open ("path_to_file", ios::trunc);
      myfile << content;
      myfile.close();
    }
    

    Similarly on reopening this view, you'll run a reload function and read the file into a variable, and set it's value into the textbox