A.Thabet
A.Thabet

Reputation: 113

highlight a word that has x and y as a position inside a qtextedit

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

Answers (1)

Alessio G. B.
Alessio G. B.

Reputation: 120

You can use QSyntaxHighlighter.

Upvotes: 1

Related Questions