Reputation: 113
I am writing a code editor using Qt. I have a problem, I am wondering how to highlight a word inside my textedit using it's x and y position (line and column) , any idea please?
Using this method i can get the position and highlight some characters, but after that if i want to display an other text all the text was been highlighted.
void MainWindow::Target(int row, int colum) { QTextEdit* TempTextEdit = ui->textEdit; QTextDocument* document = TempTextEdit->document(); QTextCursor cursor(document); //cursor.beginEditBlock(); // cursor.setPosition(0); cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, row-1); cursor.setPosition(colum); cursor.setPosition(colum+2, QTextCursor::KeepAnchor); QTextCharFormat plainFormat(cursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); colorFormat.setFontUnderline(true); cursor.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor); cursor.mergeCharFormat(colorFormat); // cursor.endEditBlock(); TempTextEdit->setTextCursor(cursor); }
Upvotes: 1
Views: 405