i'm trying to make a chat application in QT . Is it possible to read data from the text browser of QT GUI(which shows conversations) so that i can maintain a chat history...?plz help..Thank You
-
What kind of control is the text-browser? QLineEdit? – Anirudh Ramanathan Aug 03 '12 at 12:22
-
@DarkXphenomenon I think he means QTextBrowser!? – leemes Aug 03 '12 at 12:22
-
Hint: [QTextBrowser](http://doc.qt.nokia.com/4.7-snapshot/qtextbrowser.html) and parent [QTextEdit](http://doc.qt.nokia.com/4.7-snapshot/qtextedit.html). See toHtml(). And next time use manual. – roslav Aug 03 '12 at 12:25
-
@all :Thank you...Sorry I didn't see that.. – kernel Aug 03 '12 at 12:32
-
I'd rather write the messages to the history as you receive them, that also gives you better control over the format you store it in. (single messages instead of one big html blob). – Frank Osterfeld Aug 03 '12 at 12:47
2 Answers
The QTextBrowser
inherits QTextEdit
, which works on a QTextDocument
. The QTextDocument can be converted to (and saved as) HTML using QTextDocument::toHtml()
:
QTextDocument *doc = ui->textBrowser->document();
QString html = doc->toHtml();
I advise you to append to a log file every time a new message comes in / goes out, so update the QTextBrowser and the file "in parallel", and not saving the entire chat history everytime a new message appears.
To do so, open the log file and manually write the open <html>
and <body>
tags without closing them. Then append the chat log entries on the still opened file. On application exit (object destruction of the chat window or whatever), close the </body>
and </html>
tags and afterwards the file itself. This will result in a much better performance than saving the whole file for every change of the QTextBrowser widget.

- 44,967
- 21
- 135
- 183