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. Problem to save text edit content to a file
QtWS25 Last Chance

Problem to save text edit content to a file

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.4k Views
  • 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.
  • A Offline
    A Offline
    arthurguedon
    wrote on last edited by arthurguedon
    #1

    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 (fileH.open(QFile::WriteOnly)) {
          QTextStream stream(&fileH);
    
                stream << codeReH;
    
    
            }
            else {
                QMessageBox::critical(this, tr("Erreur"), tr("Impossible de sauvegarder le fichier"));
    
            }
        }
    

    Thank you in adavance !
    }

    joeQJ 1 Reply Last reply
    0
    • A arthurguedon

      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 (fileH.open(QFile::WriteOnly)) {
            QTextStream stream(&fileH);
      
                  stream << codeReH;
      
      
              }
              else {
                  QMessageBox::critical(this, tr("Erreur"), tr("Impossible de sauvegarder le fichier"));
      
              }
          }
      

      Thank you in adavance !
      }

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @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 (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                return;
      

      Just do it!

      1 Reply Last reply
      0
      • A Offline
        A Offline
        arthurguedon
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          hi
          i would try
          stream << "TEST TEST";

          and see.

          1 Reply Last reply
          0

          • Login

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