Problem to save text edit content to a file

  • I created a QFileDialog to get the user to save the content of a QTextEdit and everything works fine, I'm able to save the document however when I open it with a text editor, there is not the text from the text edit but only hexadecimals and I don't get how I could fix it, here is my code, and codeReH is a QString which contains the text from my QTextEdit :

    void FenCodeGenere::ouvrirSaveH() {

    QString nomFileH = QFileDialog::getSaveFileName(this, tr("Sauvegarder le fichier"), "",
                                                        tr("Header C++ (*.h)"));
        if (nomFileH != "") {
            QFile fileH(nomFileH);
            if ( {
          QTextStream stream(&fileH);
                stream << codeReH;
            else {
                QMessageBox::critical(this, tr("Erreur"), tr("Impossible de sauvegarder le fichier"));

    Thank you in adavance !

  • @arthurguedon
    Hi,friend, welcome devnet.

    1. What is store in codeReH? str ? or other some? can you debug it before save it?
    2. QFile open , you can try use the QIODevice::Text like the below.
     QFile file("out.txt");
     if (! | QIODevice::Text))

  • Thanks for your answer, codeReH stores the text from my QTexEdit, when I debug I don't see any problem as codeReH contains what was written in the text edit.
    I tried using the QIODevice::Text but it didn't change anything.

  • Qt Champions 2017

    i would try
    stream << "TEST TEST";

    and see.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.