Unsolved how to highlight selected text in QTextEdit?
-
I has realised a small app like NoteBook.Now,I want to hight selected text,but I have no idea about this. I create a class that inheit QSyntaxHighlighter.
//your code here .h class Myhighlitghter : public QSyntaxHighlighter { public: explicit Myhighlitghter(QTextDocument *parent=0); protected: virtual void highlightBlock(const QString &text); }; .cpp Myhighlitghter::Myhighlitghter(QTextDocument *parent) :QSyntaxHighlighter(parent) { } void Myhighlitghter::highlightBlock(const QString &text) { QTextCharFormat charformat; charformat.setFontWeight(QFont::Bold); charformat.setForeground(Qt::red); QString pattern="\\bchar\\b"; QRegExp exp(pattern); int index = text.indexOf(exp); while(index>=0) { int len=exp.matchedLength(); setFormat(index,len,charformat); index = text.indexOf(exp,index+len); } }
then I create a mainwindow that has Qtextedit and Myhighlitghter object.Now , I want to select text in textedit and hightlight them.
thank for your help
-
QTextCursor will help you to select the text, delete , replace, background color of the selected text.
-
@Ni.Sumi very thanks, I will have a try