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. File stream
QtWS25 Last Chance

File stream

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.7k 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.
  • S Offline
    S Offline
    shree krishna17
    wrote on last edited by
    #1

    How to write text in a file as user input? I want this in form system.

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

      Hi and welcome to devnet,

      Do you mean write in a QFile the content of e.g. a QTextEdit ?

      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
      1
      • S Offline
        S Offline
        shree krishna17
        wrote on last edited by
        #3

        Yes, exactly.

        1 Reply Last reply
        0
        • the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          When should the content be saved to a file?

          • When a "Save" button is pressed?
          • Always when the text is changed?
          • automatically every X seconds?

          -- No support in PM --

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ambershark
            wrote on last edited by ambershark
            #5

            Regardless of how your save is kicked off you can use this code to save the contents. Then if you want different kick off triggers you can have those call the function.

            bool save(const QTextEdit *te, const QString &filename)
            {
                QFile f(filename);
            
                // open the file .. WARNING: this will overwrite any existing
                // file.  Be careful here!
                if (!f.open(filename, QIODevice::WriteOnly | QIODevice::Text))
                    return false;
            
                // write the plain text data from the control, use toHtml() if
                // you want HTML data
                f.write(te->toPlainText(), te->toPlainText().size());
            
                // close the file
                f.close();
            
                return true;
            }
            

            Typed out of my head so may have issues, should be easy to fix when compiled if there are any typos/wrong function usage. :)

            Edit: as a better way you could change the function to take a const QString &data instead of the textedit. That is how I would do it. Then you can call it with something like save(myTextEdit->toPlainText(), "filename");.

            So you would need to change the function to look like this if you want that method:

            bool save(const QString &data, const QString &filename)
            {
                QFile f(filename);
            
                // open the file .. WARNING: this will overwrite any existing
                // file.  Be careful here!
                if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
                    return false;
            
                // write the plain text data from the control, use toHtml() if
                // you want HTML data
                f.write(data, data.size());
            
                // close the file
                f.close();
            
                return true;
            }
            

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            1
            • S Offline
              S Offline
              shree krishna17
              wrote on last edited by
              #6

              Thankyou i will try

              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