Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

with something like that:

QString myQString = <textBrowserObject>.toPlainText();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top