Nominate our 2022 Qt Champions!

Saving content from QTextEdit in QFile to a path without a QFileDialog

  • Hello everyone!

    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.


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

  • @koahnig
    The example is good, but how can save the file to a specific path?

  • Lifetime Qt Champion


    If you want your user to provide that path, use QFileDialog::getSaveFileName

  • @SGaist
    I know but exactly this i don't want...

  • Lifetime Qt Champion

    @HenrikSt. Did you read the QFile documentation as suggested by @koahnig ?
    There is an example how to open a file and to write into it:

       QFile file("out.txt"); // Replace this path with the one you want to use
        if (! | QIODevice::Text))
        QTextStream out(&file);
        out << "The magic number is: " << 49 << "\n";

  • Lifetime Qt Champion

    @HenrikSt. You can use QStandardPaths to get directories which are used to store specific data.

    QString path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + '/' + "file.txt";

  • @HenrikSt.

    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.

    QFile file("c:/data/myNewFolder/out.txt");

    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.

Log in to reply