user1233963
user1233963

Reputation: 1480

QSyntaxHighlighter not working for form created QTextEdit

As the title suggests, highlighting doesn't seem to work with form created QTextEdit. My QSyntaxHighlighter derrivate class is the one from Qt docs and my code (the one that doesn't work):

ui->setupUi(this);
HtmlHighlighter hl(ui->textEdit->document());

but if I do this it works fine:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow win;
    win.show();
    QTextEdit editor;
    HighLighter highlighter(editor.document());
    editor.show();

    return app.exec();
}

Is there any way to get it to work with the form generated one?

Upvotes: 1

Views: 292

Answers (1)

phyatt
phyatt

Reputation: 19112

Your highlighter is going out of scope at the end of the constructor. Put it on the heap and make it a member variable, and it should work.

class MainWindow
{
//...

private:
    HtmlHighlighter * h1;
}

Then in your cpp file:

ui->setupUi(this);
hl = new HtmlHighlighter(ui->textEdit->document());

Hope that helps.

Upvotes: 2

Related Questions