PDA

View Full Version : QTextEdit contents changed highlight the color



sujan.dasmahapatra
8th June 2011, 13:38
Dear Friends
I need your help in implementing a TextEdit….I have the functionality of saving option of the text available in the TextEdit. But everytime user loads a file and when starts editing the file the contents changed in the file should be highlighted with some color. And when save it it should return back to the original black & white color.
I am not getting any idea about this. Any suggestions would be highly appreciated.

QString contents;
QTextEdit *textEdit = new QTextEdit(this);
textEdit->setPlainText(contents);
textEdit->show();

Now after displaying the contents user wants to modify the contents, so all the contents only the modified part should be highlighted with some color, and when save it then come back to the normal mode.

Santosh Reddy
8th June 2011, 16:29
Here is a tip:

Set a default color format to QTextEdit using (say default color)


void QTextEdit::setTextColor ( const QColor & c )

once the document is loaded, the the color to some editing color (color of your choice), so any text typed here onward will be in this color. When user saves the data then save text to file, fallback to the default color, clear the text in QTextEdit, load the text again, and set editing color again.

sujan.dasmahapatra
8th June 2011, 17:04
Thats a good tip but I dont want to play with the text color but its background color or highlighting the text. What can be done for that.. Thanks for your reply.

Santosh Reddy
8th June 2011, 18:31
Consider using one of the following.


QColor QTextEdit::textBackgroundColor();
void QTextEdit::setFontUnderline (bool underline); // text underline
void QTextEdit::setFontPointSize(qreal s); // text size
void QTextEdit::setFontWeight (int weight); // Normal, Bold etc
void QTextEdit::setCurrentFont(const QFont & f);

Also read QTextEdit