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 : save text from text Browser
Forum Updated to NodeBB v4.3 + New Features

Problem : save text from text Browser

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.6k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    musti
    wrote on last edited by
    #1

    hello
    i tried this code to save text from text browser but it only create a blank text document
    @
    void MainWindow::fileSave()
    {
    QFile sFile (mFilename); //mFilename is a QString

            if( !sFile.open (QFile::WriteOnly | QFile::Text)) 
                                        
                                                            return fileSaveAs();
    
            if(sFile.open(QFile::WriteOnly | QFile::Text)){
    
                               QTextStream out(&sFile);
    
                              out<<textView->toPlainText();  ;
    
                sFile.flush();
                sFile.close();
            }};
    
       void MainWindow::fileSaveAs()
        {
    

    QString File = QFileDialog::getSaveFileName(this, tr("Save as..."),QString(), tr("txt (*.txt);")) ;

                if (!File.isEmpty()){
                    mFilename = File ;
                    fileSave();
    

    }};@

    i made a button that open a file in the text browser and it work but when i save it again it become blank .

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You don't do anything if open fails. You should at least use a QMessageBox to show a warning.

      On a side note, this pretty much continues this "thread":http://qt-project.org/forums/viewthread/43815/ you should have rather kept it there.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        musti
        wrote on last edited by
        #3

        hi
        i have no problem with open file my problem is with saving it .
        i can save a plaintext with the same code but here i have a textbrowser .
        what should i do ?
        thank you

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

          QTextBrowser is a QTextEdit so it should behave pretty much the same.

          One quick way to verify the content of your QTextBrowser is to

          @qDebug() << textView->toPlainText();@

          Anyway, it's still not a clean technique to open a file twice.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            musti
            wrote on last edited by
            #5

            thank you so much :)
            the code became :
            @
            void MainWindow::fileSave()
            {
            QFile sFile(mFilename);

                    if(sFile.open(QFile::WriteOnly | QFile::Text&#41;){
                                            QTextStream out(&sFile);
            
                        out <<textView->toPlainText();
                     
            
                        sFile.flush();
                        sFile.close();
                    }};
            

            @
            i wish to become an expert like you someday :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should still have a at least something that tells you if open failed e.g. you user choose a read-only file.

              Keep practicing young one ;) don't hesitate to ask questions to more experienced developers and read documentation (books, Qt's documentation examples etc.)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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