Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TextChanged triggered with syntaxhighlighter->rehighlight()
Forum Updated to NodeBB v4.3 + New Features

TextChanged triggered with syntaxhighlighter->rehighlight()

Scheduled Pinned Locked Moved General and Desktop
21 Posts 2 Posters 8.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vogtinator
    wrote on 5 Sept 2013, 11:25 last edited by
    #10

    The parent is QPlainTextEditor::document and it doesn't even get to highlightBlock(text). My workaround is
    @ ui->codeEdit->blockSignals(true);
    delete highlighter;
    delete ui;@

    which works 100%. Simplest solution would be not to trigger textChanged() in the destructor or not by the QSyntaxHighlighter at all.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Sept 2013, 11:39 last edited by
      #11

      The way I see it:

      highlighter is destroyed

      document must be "cleaned" from highlighting

      documentChanged is emitted

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vogtinator
        wrote on 5 Sept 2013, 13:29 last edited by
        #12

        Here, a complete stacktrace:
        The MainWindow deletes the QSyntaxHighlighter, which triggers textChanged(), which calls QSyntaxHighlighter::rehighlight(), which seems to be pure virtual as being destroyed.
        @0 raise /lib64/libc.so.6
        1 abort /lib64/libc.so.6
        2 __gnu_cxx::__verbose_terminate_handler()
        3 ??
        4 std::terminate()
        5 __cxa_pure_virtual
        6 ?? /usr/lib64/libQtGui.so.4
        7 ?? /usr/lib64/libQtGui.so.4
        8 QSyntaxHighlighter::rehighlight()
        9 SteveHighlighter::resetHighlight
        10 MainWindow::textChanged mainwindow.cpp 187
        11 MainWindow::qt_static_metacall
        12 QMetaObject::activate(QObject*, QMetaObject const*, int, void**)
        13 QMetaObject::activate(QObject*, QMetaObject const*, int, void**)
        14 ??
        15 QTextControl::qt_metacall(QMetaObject::Call, int, void**)
        16 QMetaObject::activate(QObject*, QMetaObject const*, int, void**)
        17 ??
        18 QSyntaxHighlighter::setDocument(QTextDocument*)
        19 QSyntaxHighlighter::~QSyntaxHighlighter()
        20 SteveHighlighter::~SteveHighlighter stevehighlighter.h
        21 SteveHighlighter::~SteveHighlighter stevehighlighter.h
        22 MainWindow::~MainWindow mainwindow.cpp
        23 main@

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 5 Sept 2013, 20:32 last edited by
          #13

          Just thought about something... Why do you need to call rehighlight after resetErrorHighlighting ? Shouldn't that cleanup also do the highlighting ?

          Also, could you share the code of the highlighter ? I just tried with a minimal highlighter and everything goes well

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vogtinator
            wrote on 5 Sept 2013, 22:38 last edited by
            #14

            bq. Just thought about something… Why do you need to call rehighlight after resetErrorHighlighting ? Shouldn’t that cleanup also do the highlighting ?

            Yes, and it does it with rehighlight :-)

            @#include "stevehighlighter.h"

            SteveHighlighter::SteveHighlighter(QPlainTextEdit *editor)
            : QSyntaxHighlighter(editor->document()), parent(editor)
            {
            QTextCharFormat format;

             format.setForeground(QColor(0, 128, 0));
             format.setFontWeight(QFont::Bold);
             setFormat(TOK_KEYWORD, format);
            
             format.setForeground(QColor(192, 16, 112));
             format.setFontWeight(QFont::Bold);
             setFormat(TOK_CONDITION, format);
            
             format.setForeground(QColor(128, 0, 0));
             format.setFontWeight(QFont::Bold);
             setFormat(TOK_INSTRUCTION, format);
            

            }

            void SteveHighlighter::setFormat(Token what, const QTextCharFormat &format)
            {
            this->format[what] = format;
            rehighlight();
            }

            void SteveHighlighter::resetHighlight()
            {
            rehighlight();
            }@

            Header:
            @#ifndef STEVEHIGHLIGHTER_H
            #define STEVEHIGHLIGHTER_H

            #include <QPlainTextEdit>
            #include <QSyntaxHighlighter>

            enum Token {
            TOK_KEYWORD = 0,
            TOK_CONDITION = 1,
            TOK_INSTRUCTION = 2
            };

            class SteveHighlighter : public QSyntaxHighlighter
            {
            Q_OBJECT

            public:
            SteveHighlighter(QPlainTextEdit *editor);
            void highlightBlock(const QString &text) override {};
            void highlight(int line, const QTextCharFormat &format, const QString &what = "") {};
            void resetHighlight();
            void setFormat(Token what, const QTextCharFormat &format);

            private:
            int highlight_line;
            QString highlight_str;
            QPlainTextEdit *parent;
            QTextCharFormat format[TOK_INSTRUCTION + 1];
            };
            #endif // STEVEHIGHLIGHTER_H@

            I removed some irrelevant parts, but I also noticed after removal of the constructor code, it no longer crashes. I didn't expect that.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 6 Sept 2013, 07:13 last edited by
              #15

              The thing is, you are creating an infinite loop calling rehighlight in the slot triggered by textChanged, you should rather do everything in one pass (doesn't mean that you can't split the highlighting part in several functions)
              or delay the highlighting a bit.

              One thing to try to change: pass the text edit directly as parent (third constructor)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vogtinator
                wrote on 6 Sept 2013, 11:42 last edited by
                #16

                bq. The thing is, you are creating an infinite loop calling rehighlight in the slot triggered by textChanged, you should rather do everything in one pass (doesn’t mean that you can’t split the highlighting part in several functions) or delay the highlighting a bit.

                But that's not the cause of the crash.

                bq. One thing to try to change: pass the text edit directly as parent (third constructor)

                Tried that. Doesn't work, QPlainTextEdit != QTextEdit. QPlainTextEdit gets implicitly converted to QObject.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 6 Sept 2013, 12:39 last edited by
                  #17

                  Indeed... But why are you keeping a pointer to the parent ? You don't have any destructor and IIRC the default generated should delete all the member so deleting the parent variable which hold a pointer to the QPlainTextEdit which is the parent (indirectly) of the highlighter might be a problem...

                  Sorry I mixed the two classes

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Vogtinator
                    wrote on 6 Sept 2013, 12:44 last edited by
                    #18

                    I'm 100% sure a default constructor doesn't delete any objects a member pointer points to, only the pointer itself. A manual-specified destructor always runs before destroying the non-static members.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 6 Sept 2013, 19:40 last edited by
                      #19

                      You're right ! The object pointed to are not destroyed by the default constructor.

                      There must be something else... What did you removed from the constructor that avoided the crash ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        Vogtinator
                        wrote on 6 Sept 2013, 19:48 last edited by
                        #20

                        The three
                        @ QTextCharFormat format;
                        format.setForeground(QColor(0, 128, 0));
                        format.setFontWeight(QFont::Bold);
                        setFormat(TOK_KEYWORD, format);@

                        blocks. Without them, it works. Might be because there's a similiar function setFormat but with different arguments in QSyntaxHighlighter. But then, it should also crash without these lines.
                        I could narrow it down further to rehighlight() in setFormat, but I really don't know why.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 10 Sept 2013, 21:47 last edited by
                          #21

                          Strange... Can't reproduce it on OS X

                          By the way, I would recommend using a QHash, you'll be more flexible if you add new Tokens

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          19/21

                          6 Sept 2013, 19:40

                          • Login

                          • Login or register to search.
                          19 out of 21
                          • First post
                            19/21
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved