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. QTextEdit::append should have a QColor or QTextCharFormat parameter
QtWS25 Last Chance

QTextEdit::append should have a QColor or QTextCharFormat parameter

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 955 Views
  • 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.
  • MegamouseM Offline
    MegamouseM Offline
    Megamouse
    wrote on last edited by
    #1

    Hi,

    I stumbled upon a use case where i would like to add text in a new color at the end of my document without losing or repainting my selection or scrolling in case of isBottom.

    This is basically what append does. But if i wanna change my new textcolor with setTextColor I repaint my selection.

    What is the best way of doing this? Or is it simply unimplemented?

    thx Megamouse

    1 Reply Last reply
    0
    • MegamouseM Offline
      MegamouseM Offline
      Megamouse
      wrote on last edited by Megamouse
      #2

      To be precise...
      It would be necessary to add the color part here (see the comments):

      void QTextEdit::append(const QString &text) // add param: const QColor& color
      {
          Q_D(QTextEdit);
          const bool atBottom = isReadOnly() ?  d->verticalOffset() >= d->vbar->maximum() :
                  d->control->textCursor().atEnd();
          d->control->append(text); // add color
          if (atBottom)
              d->vbar->setValue(d->vbar->maximum());
      }
      
      void QWidgetTextControlPrivate::append(const QString &text, Qt::TextFormat format)
      {
          QTextCursor tmp(doc);
          tmp.beginEditBlock();
          tmp.movePosition(QTextCursor::End);
      	
          // Old code:
          //if (!doc->isEmpty())
          //    tmp.insertBlock(cursor.blockFormat(), cursor.charFormat());
          //else
          //    tmp.setCharFormat(cursor.charFormat());
      	
          // New code:
          QTextCharFormat newCharFormat = cursor.charFormat();
          if (color.isValid())
              newCharFormat.setForeground(color);
          if (!doc->isEmpty())
              tmp.insertBlock(cursor.blockFormat(), newCharFormat);
          else
              tmp.setCharFormat(newCharFormat);
      	
          // preserve the char format
          QTextCharFormat oldCharFormat = cursor.charFormat();
      #ifndef QT_NO_TEXTHTMLPARSER
          if (format == Qt::RichText || (format == Qt::AutoText && Qt::mightBeRichText(text))) {
              tmp.insertHtml(text);
          } else {
              tmp.insertText(text);
          }
      #else
          Q_UNUSED(format);
          tmp.insertText(text);
      #endif // QT_NO_TEXTHTMLPARSER
          if (!cursor.hasSelection())
              cursor.setCharFormat(oldCharFormat);
          tmp.endEditBlock();
      }
      
      1 Reply Last reply
      0
      • MegamouseM Offline
        MegamouseM Offline
        Megamouse
        wrote on last edited by
        #3

        On another note: The append function should get a flag that skips the insertBlock

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Can you explain what your use case is ?

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

          MegamouseM 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Can you explain what your use case is ?

            MegamouseM Offline
            MegamouseM Offline
            Megamouse
            wrote on last edited by
            #5

            @SGaist a log that has different colors depending on error types etc.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Looks rather like a job for QSyntaxHighlighter.

              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
              2
              • MegamouseM Offline
                MegamouseM Offline
                Megamouse
                wrote on last edited by
                #7

                I am not sure but I get the feeling the Syntaxhighlighter is too slow for a log

                mrjjM 1 Reply Last reply
                0
                • MegamouseM Megamouse

                  I am not sure but I get the feeling the Syntaxhighlighter is too slow for a log

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Megamouse
                  Hi
                  Just as note. I found QTextEdit with append too slow with huge logs
                  files and switch to a listView with a delegate.

                  1 Reply Last reply
                  3
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Megamouse said in QTextEdit::append should have a QColor or QTextCharFormat parameter:

                    I am not sure but I get the feeling the Syntaxhighlighter is too slow for a log

                    That's one crucial detail you forgot to say in your problem description. @mrjj is right here, to show logs like that, the model/view architecture will work better.

                    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
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      It you stick with QTextEdit the QTextCursor interface is public so you can use QTextEdit::cursor() and do exactly what QWidgetTextControlPrivate::append does changing what you need

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      3

                      • Login

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