Congratulations to our 2022 Qt Champions!

Disappearing lines when using a syntax highlighter

  • I'm a developer of the TeX editor project TeXworks that is based on the Qt framework. About half a year ago, the problem of the disappearing lines has been reported [1], but now I finally managed to reproduce it in a minimal example.

    Source Code (main.cpp):
    #include <QtGui>

    // Syntax Highlighter code taken from
    // (with modified pattern to suite test file)
    class MyHighlighter : public QSyntaxHighlighter
    MyHighlighter(QTextEdit * parent) : QSyntaxHighlighter(parent) {}
    void highlightBlock(const QString &text) {
    QTextCharFormat myClassFormat;
    QString pattern = "%.*$";

    QRegExp [removed]pattern);
    int index = text.indexOf(expression);
    while (index >= 0) {
    int length = expression.matchedLength();
    setFormat(index, length, myClassFormat);
    index = text.indexOf(expression, index + length);

    int main(int argc, char *argv[])
    QApplication app(argc, argv);
    QTextEdit widget;
    QString contents;
    MyHighlighter highlighter(&widget);

    // Load file
    QFile f("problem.txt");
    if (! {
    qDebug() << "Could not open file";
    return 1;

    // Conveniance only (has no effect on issue)
    widget.move(0, 0);

    // Swapping the following two lines works around the issue (but is not
    // always applicable in real-life apps);

    // Run app
    return app.exec();

    Test file: "problem.txt":

    Here's the problem: when I use a QSyntaxHighlighter-derived class and call highlighter.rehighlight() in the initialization, several lines simply vanish in the initial display. One has to resize the window horizontally (presumably to cause a relayout of the text) to show those lines.

    This seems to depend on the Qt version used. So far, I've tested the following ones:

    • gcc 4.5.2
      ** Qt 4.6.3 (debug) works
      ** Qt 4.7.2 (debug) fails, missing lines: 144--147, 154--157, 160--161, 169--181
    • gcc 4.6.0
      ** Qt 4.7.3 fails, missing: 155-157, 160-161, 169-181

    I'm not sure if this is also system-dependent?

    A few things to note:

    • this is independent of the window's initial size, but manual horizontal resize works around the problem
    • moving rehighlight() in front of show() apparently solve the problem as well (though this might not be possible in real-life if rehighlight is tied to signals and/or other parts of the code, e.g., for document parsing)
    • the missing lines depend on the highlighting definition (changing "%.*$" to "%.+$" made lines 137--143 disappear with Qt 4.7.2)
    • the cursor disappears when moving to hidden lines with the arrow keys (the real app, TeXworks, crashes, presumably due to the "highlight current line" feature using code from

    Some additional debugging suggests that the text is properly split into QTextBlocks, but that those are not (all) laid out properly into QTextLines. But that internal layout code is fairly complex...

    Does anyone have any hint as to where this issue is coming from or how to (properly) solve this?

    Thanks in advance,


  • Have you considered "filing a bug report":

    Forum posts are known to vanish, and it would be too bad about your extensive testing if it would do so.

  • Thanks, I did that just now:

    I didn't have particularly good experience with the bug tracker, though (issues uncommented for months, or closed without any reason apparent to me), so I thought I'd try asking here, first.

  • sorry for reviving an old thread, but I've just stumbled upon the same bug (Qt 4.7.4, Win32).

    It looks like it has something to do with calling rehighlight() at an inappropriate time. Removing the calls to rehighlight() or manually triggering it (i.e. upon button press) gets rid of the problem, but is impractical.

    One possible workaround (although ugly):

    @void CMyWidgetW::rulesChanged()

Log in to reply