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. Can't set formatting on the QTextEdit
Forum Update on Monday, May 27th 2025

Can't set formatting on the QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.5k 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.
  • O Offline
    O Offline
    oneeyeman1
    wrote on last edited by
    #1

    Hi, ALL,
    I have Qt 5.7.1 on Gentoo Linux and I have a following weird problem.

    I have QTextEdit control and trying to set different styling for it.
    When I try to set the styling somewhere in the middle of the text, everything works.
    When I try to set the styling at the end of the text - it doesn't.

    I tried both setCharFormat() and mergeCharFormat() call with the simplest text and background color change.

    Does anybody knows how do I change the styling at the end of the text? It should be picked up by inserting the text programmatically .

    Thank you in advance!

    Gojir4G JonBJ 2 Replies Last reply
    0
    • O oneeyeman1

      Hi, ALL,
      I have Qt 5.7.1 on Gentoo Linux and I have a following weird problem.

      I have QTextEdit control and trying to set different styling for it.
      When I try to set the styling somewhere in the middle of the text, everything works.
      When I try to set the styling at the end of the text - it doesn't.

      I tried both setCharFormat() and mergeCharFormat() call with the simplest text and background color change.

      Does anybody knows how do I change the styling at the end of the text? It should be picked up by inserting the text programmatically .

      Thank you in advance!

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @oneeyeman1 Hi,

      What do you mean by "end of the text" ? Last word, or line ?
      Can you provide the code showing how you are applying your styling ?

      O 1 Reply Last reply
      3
      • O oneeyeman1

        Hi, ALL,
        I have Qt 5.7.1 on Gentoo Linux and I have a following weird problem.

        I have QTextEdit control and trying to set different styling for it.
        When I try to set the styling somewhere in the middle of the text, everything works.
        When I try to set the styling at the end of the text - it doesn't.

        I tried both setCharFormat() and mergeCharFormat() call with the simplest text and background color change.

        Does anybody knows how do I change the styling at the end of the text? It should be picked up by inserting the text programmatically .

        Thank you in advance!

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @oneeyeman1
        Do you mean you try to leave the text with some style at the end on "nothing" and then later just append text, expecting it to pick up that styling? At a guess, you need to apply the styling at the instant the new text is appended, so it has some content to apply to?

        O 1 Reply Last reply
        1
        • Gojir4G Gojir4

          @oneeyeman1 Hi,

          What do you mean by "end of the text" ? Last word, or line ?
          Can you provide the code showing how you are applying your styling ?

          O Offline
          O Offline
          oneeyeman1
          wrote on last edited by oneeyeman1
          #4

          Hi,
          @Gojir4 said in Can't set formatting on the QTextEdit:

          @oneeyeman1 Hi,

          What do you mean by "end of the text" ? Last word, or line ?
          Can you provide the code showing how you are applying your styling ?

          Consider this:

          1. I have a QTextEdit.
          2. This control contains a phrase "A quick brown fox jumps over the lazy dog."
          3. The control itself is editable and the (text) cursor is blinking at the end of the phrase (after the "." symbol) waiting for input.

          At this point I can successfully set the coloring to any words in the sentence.
          However, when I try to set the formatting to where my cursor is - so that the next text I add will be in that color scheme - I can't. The text is still be added as black on white.

          So the meaning of it is - when my cursor positioned after the last character I typed.

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            What about
            https://doc.qt.io/qt-5/qplaintextedit.html#setCurrentCharFormat
            so it uses your format as the default one when inserting text ?

            O 1 Reply Last reply
            1
            • JonBJ JonB

              @oneeyeman1
              Do you mean you try to leave the text with some style at the end on "nothing" and then later just append text, expecting it to pick up that styling? At a guess, you need to apply the styling at the instant the new text is appended, so it has some content to apply to?

              O Offline
              O Offline
              oneeyeman1
              wrote on last edited by
              #6

              Hi,
              @JonB said in Can't set formatting on the QTextEdit:

              @oneeyeman1
              Do you mean you try to leave the text with some style at the end on "nothing" and then later just append text, expecting it to pick up that styling? At a guess, you need to apply the styling at the instant the new text is appended, so it has some content to apply to?

              Well, yes - I'm trying to apply the formatting on "nothing" for the text I will add in the future.
              And yes - the text is appended immediately after I set the formatting.

              But I don't understand what you mean by saying "so it has some content to apply to?" I can append the text at any time I want and I WILL expect for the formatting to be applied.

              Thank you.

              JonBJ 1 Reply Last reply
              0
              • O oneeyeman1

                Hi,
                @JonB said in Can't set formatting on the QTextEdit:

                @oneeyeman1
                Do you mean you try to leave the text with some style at the end on "nothing" and then later just append text, expecting it to pick up that styling? At a guess, you need to apply the styling at the instant the new text is appended, so it has some content to apply to?

                Well, yes - I'm trying to apply the formatting on "nothing" for the text I will add in the future.
                And yes - the text is appended immediately after I set the formatting.

                But I don't understand what you mean by saying "so it has some content to apply to?" I can append the text at any time I want and I WILL expect for the formatting to be applied.

                Thank you.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @oneeyeman1
                So I'm purely guessing. If I understand right, you'd like to set the font on some text which isn't yet there, at the end of the line. So there's nothing to set the font on right now. Maybe that doesn't work, maybe the font change only takes/persists if there is some text to put it on. Maybe you have to save up what font you want and apply it when some text later arrives.

                Like I said I'm purely guessing. Take what I say with a pinch of salt.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  What about
                  https://doc.qt.io/qt-5/qplaintextedit.html#setCurrentCharFormat
                  so it uses your format as the default one when inserting text ?

                  O Offline
                  O Offline
                  oneeyeman1
                  wrote on last edited by
                  #8

                  Hi,
                  @mrjj said in Can't set formatting on the QTextEdit:

                  Hi
                  What about
                  https://doc.qt.io/qt-5/qplaintextedit.html#setCurrentCharFormat
                  so it uses your format as the default one when inserting text ?

                  I will try that tonight.
                  I presume it is available thru the QTextEdit as well?

                  Thank you.

                  mrjjM 1 Reply Last reply
                  0
                  • O oneeyeman1

                    Hi,
                    @mrjj said in Can't set formatting on the QTextEdit:

                    Hi
                    What about
                    https://doc.qt.io/qt-5/qplaintextedit.html#setCurrentCharFormat
                    so it uses your format as the default one when inserting text ?

                    I will try that tonight.
                    I presume it is available thru the QTextEdit as well?

                    Thank you.

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

                    @oneeyeman1
                    Hi
                    Yes it is.
                    https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                    It sounds like you want to set the format for new text inserted and that would do it.

                    O 2 Replies Last reply
                    3
                    • mrjjM mrjj

                      @oneeyeman1
                      Hi
                      Yes it is.
                      https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                      It sounds like you want to set the format for new text inserted and that would do it.

                      O Offline
                      O Offline
                      oneeyeman1
                      wrote on last edited by
                      #10

                      Hi,
                      @mrjj said in Can't set formatting on the QTextEdit:

                      @oneeyeman1
                      Hi
                      Yes it is.
                      https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                      It sounds like you want to set the format for new text inserted and that would do it.

                      Thank you.
                      I will try that tonight and let you know.

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @oneeyeman1
                        Hi
                        Yes it is.
                        https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                        It sounds like you want to set the format for new text inserted and that would do it.

                        O Offline
                        O Offline
                        oneeyeman1
                        wrote on last edited by
                        #11

                        Hi,
                        @mrjj said in Can't set formatting on the QTextEdit:

                        @oneeyeman1
                        Hi
                        Yes it is.
                        https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                        It sounds like you want to set the format for new text inserted and that would do it.

                        I just tried to call m_edit->setCurrentCharFormat();
                        Nothing happened.

                        I have a Qt 5.7.1 on Gentoo Linux and I'm trying to mix up formatting inside the text and at the end of the text.
                        Inside the text everything works. Trying to change it at the end does not.

                        Maybe it was a bug in that specific version of Qt and it was later fixed?

                        Or maybe that call is conditional and it checks something that needs to be set?

                        Any idea of what I might try next?

                        Thank you.

                        O 1 Reply Last reply
                        0
                        • O oneeyeman1

                          Hi,
                          @mrjj said in Can't set formatting on the QTextEdit:

                          @oneeyeman1
                          Hi
                          Yes it is.
                          https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                          It sounds like you want to set the format for new text inserted and that would do it.

                          I just tried to call m_edit->setCurrentCharFormat();
                          Nothing happened.

                          I have a Qt 5.7.1 on Gentoo Linux and I'm trying to mix up formatting inside the text and at the end of the text.
                          Inside the text everything works. Trying to change it at the end does not.

                          Maybe it was a bug in that specific version of Qt and it was later fixed?

                          Or maybe that call is conditional and it checks something that needs to be set?

                          Any idea of what I might try next?

                          Thank you.

                          O Offline
                          O Offline
                          oneeyeman1
                          wrote on last edited by
                          #12

                          Hi,
                          @oneeyeman1 said in Can't set formatting on the QTextEdit:

                          Hi,
                          @mrjj said in Can't set formatting on the QTextEdit:

                          @oneeyeman1
                          Hi
                          Yes it is.
                          https://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat

                          It sounds like you want to set the format for new text inserted and that would do it.

                          I just tried to call m_edit->setCurrentCharFormat();
                          Nothing happened.

                          I have a Qt 5.7.1 on Gentoo Linux and I'm trying to mix up formatting inside the text and at the end of the text.
                          Inside the text everything works. Trying to change it at the end does not.

                          Maybe it was a bug in that specific version of Qt and it was later fixed?

                          Or maybe that call is conditional and it checks something that needs to be set?

                          Any idea of what I might try next?

                          Thank you.

                          I think its an issue with the version I have (5.7.1).

                          I just took a look at https://github.com/qt/qtbase/blob/5.7/src/gui/text/qtextcursor.cpp.

                          Lines 1016-1019 reads:

                          if (d->position == d->anchor) {
                              d->currentCharFormat = d->priv->formatCollection()->indexForFormat(format);
                              return;
                          }
                          

                          so when the current position is an anchor, retrieve the format and return. No setting will occur.

                          I don't know how to workaround this though...

                          That is unless I'm reading the code correctly.

                          Anyone have an idea?

                          TIA!

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Snorkelbuckle
                            wrote on last edited by
                            #13

                            Did anybody get this figured out?

                            BTW, I'm having the same problem with Qt 5.13.1, here is my related post: https://forum.qt.io/topic/107173/how-to-insert-italic-text-to-qplaintextedit

                            I can't get any new text that is inserted to contain a specific format.

                            1 Reply Last reply
                            0

                            • Login

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