Enable/disable syntax highlight in QTextEdit

c++, qt

Solution

Rather than trying to delete the highlighter, you should use:

highlighter->setDocument(0);

Edit: Deleting the highlighter also works, so you have probably inadvertently set another `QSyntaxHighlighter` on the document, or you are not disabling or deleting the correct one.

Because the syntax highlighter installs itself as a child of the document, you can retrieve it / them with `findChild` / `findChildren`:

foreach(QSyntaxHighlighter* highlighter,
        ui->textEdit->findChildren<QSyntaxHighlighter*>()) {
    delete highlighter;
}

Problem

So, I'm writing a simple code editor in C++ and Qt. I have managed to create some syntax highlighting (SH) rules by using the QSyntaxHighlighter class. Now, I want to enable and disable this feature. To enable SH on a QTextEdit, I have a pointer: ``` SyntaxHighlight *highlighter; highlighter = new SyntaxHighlight(editor->document()); ``` All I have to do, is somehow make this pointer point to nothing. But I have tried to make it point to NULL and 0 without any result. Have also creates a deconstructor, and used `delete highlighter`. Nothing seems to work. Any ideas? Please let me know if I should supply more code.

Original source