Saving content from QTextEdit in QFile to a path without a QFileDialog
I want to save the Content of QTextEdit to path (e.g. C:/Users/Desktop/Save). The Format of the file should be .htm
How can i do that? I don't want to open the QFileDialog.
This function should be used as a recovery file function. The User don't want to save the file, the program produce a file. If the user want to edit the not save file he can use the recovery file which the program automatically when the Programm closed.
koahnig last edited by
QFileDialog gives you simply a name, but it does not open a file anyway.
QFile is used to open a file. Check out the examples in the description. There is an input as well as an output example given.
With QTextEdit::toHtml you can get the data and write to the desired file.
The example is good, but how can save the file to a specific path?
If you want your user to provide that path, use QFileDialog::getSaveFileName
I know but exactly this i don't want...
QFile file("out.txt"); // Replace this path with the one you want to use if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out(&file); out << "The magic number is: " << 49 << "\n";
QString path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + '/' + "file.txt";
koahnig last edited by koahnig
You got a number of different possibilities suggested. Possibly you are overwhelmed by the different things. Sometimes it is hard for the people answering to know where the real problem was/is.
I am not sure now if this might be too detailed for you. Anyway I move forward.
I would suggest, if you have not done yet, The example from QFile as also listed by @jsulm and play a bit around. Just change the file name to somthing you need. E.g.
That is a possibility to specify a complete (absolute) file name.
If you are a beginner, I was in that stage before as all the others, there might be a problem with the not existing folder name, if your file not created. There is QDir::mkPath, which is slightly different from mkDir. It may help you in code to ensure that you actually can create the file.
The other thing driving me personally nuts is the stupid folder separator for windows, which is a back slash '\'. However, in the mean time you can easily substitute with a forward slash also for most things in Windows (especially Qt). This also part of some of the suggestions.
If you have difficulties sometimes it helps for others to post a short section of code and the error message.