File stream
Moved
Unsolved
General and Desktop
-
wrote on 30 Dec 2016, 22:47 last edited by
How to write text in a file as user input? I want this in form system.
-
-
wrote on 2 Jan 2017, 06:02 last edited by
Yes, exactly.
-
wrote on 2 Jan 2017, 07:03 last edited by
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?
-
wrote on 2 Jan 2017, 21:04 last edited by ambershark 1 Feb 2017, 21:07
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 likesave(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; }
-
wrote on 3 Jan 2017, 04:58 last edited by
Thankyou i will try
6/6