[Solved] Trouble with QSyntaxHighlighter



  • Hello,

    First post here, and just started learning Qt. I'm a hobbyist programmer and computer tinkerer (those aren't part of my day job) and decided it was time I picked up something new to learn.

    Browsing through some documentation, I stumbled upon the QSyntaxHighlighter class. I thought that sounded neat and I wanted to try it out, but had no luck. The code below reproduces the problem I am having, which is that the highlightBlock() function is never called/reached. It could be that the implementation inside highlightBlock() isn't quite right (attempted to color a single line C++ style comment), but that is secondary to my main problem. I hastily copied that out of an example and can tinker with that later. Right now I just need execution to reach that point.

    highlighter.h
    @
    #ifndef HIGHLIGHTER_H
    #define HIGHLIGHTER_H

    #include <QSyntaxHighlighter>

    class Highlighter : public QSyntaxHighlighter
    {
    Q_OBJECT
    public:
    explicit Highlighter(QObject *parent = 0);

    protected:
    void highlightBlock(const QString &text);

    signals:

    public slots:

    };

    #endif // HIGHLIGHTER_H
    @

    highligher.cpp
    @
    #include "highlighter.h"

    Highlighter::Highlighter(QObject *parent) :
    QSyntaxHighlighter(parent)
    {
    }

    void Highlighter::highlightBlock(const QString &text) {
    QRegExp pattern("//[^\n]*");
    QTextCharFormat format;

    format.setForeground(Qt::blue);

    int index = pattern.indexIn(text);
    while (index > 0) {
    int length = pattern.matchedLength();
    setFormat(index, length, format);
    index = pattern.indexIn(text, index + length);
    }
    @

    main.cpp
    @
    #include <QtGui/QApplication>
    #include <QTextEdit>
    #include "highlighter.h"

    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit();
    // Highlighter *highlighter = new Highlighter(editor); <-- This also doesn't work
    Highlighter *highlighter = new Highlighter(editor->document());

    editor->show();
    app.exec();
    }
    @

    If I put a break point in Highlighter::highlightBlock and debug the application, the break point is never reached. The "documentation":http://doc.qt.nokia.com/4.7/qsyntaxhighlighter.html makes it seam pretty easy:
    1.) Inherit from QSyntaxHighlighter and implement highlightBlock()
    2.) Create an instance of your new highlighter, setting the textEdit you want to highlight as its parent.

    Since the process was so simple, I'm not sure where I went wrong or where to look next. Any help is appreciated. I'm running an Ubuntu distribution, and the package manager tells me my Qt version is 4:4.7.0ubuntu4.2

    Thanks,



  • Hi,

    bq. If I put a break point in Highlighter::highlightBlock and debug the application, the break point is never reached.

    You are never calling that function.
    According to the above code

    @Highlighter *highlighter = new Highlighter(editor->document());@

    You are creating an object of the class Highlighter and initializing it by using the new operator and calling the constructor which doen't take QTextDocument as a parameter.

    Using the object call the function. For example:
    @highlighter->highlightBlock(editor->toPlainText);@

    Thanks,

    Sri



  • [quote author="AS.Sri" date="1292413581"]
    You are creating an object of the class Highlighter and initializing it by using the new operator and calling the constructor which doen't take QTextDocument as a parameter.
    [/quote]

    QSyntaxHighlighter has three constructors, one that takes a QObject* as a parent, one that takes a QTextDocument*, and one that takes a QTextEdit*.

    In my code above, I provided the constructor taking a QObject*, but not the one taking QTextDocument*. After adding that constructor, the highlighter works!

    As for calling highlightBlock() directly, highlightBlock() is a protected member. It is not designed to be called directly by the client. My understanding is that the parent object will call it automatically as text is entered or modified, and indeed this must be the case since my highlighter is working.

    I made one change to get the implementation of the highlightBlock() function to make it work:

    @ int index = pattern.indexIn(text);
    while (index > 0) {
    ...
    }@

    The condition needed to be
    @ while (index >= 0) @
    in order for a comment at the beginning of a block to work.

    Thanks for pointing out the incorrect constructor.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.