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. How to change the colour of a QTextBlock in a QTextEdit

How to change the colour of a QTextBlock in a QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 943 Views 1 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.
  • O Offline
    O Offline
    Olivier Ronat
    wrote on last edited by
    #1

    Hello
    I want to change the colour of the text (black to red) in a block of a QTextEdit and I wrote the following function :

    setColourBlock(QTextBlock bloc)
    {
    QTextCursor cursor(bloc);
    cursor.movePosition(QTextCursor::StartOfBlock,QTextCursor::MoveAnchor);
    cursor.movePosition(QTextCursor::EndOfBlock,QTextCursor::KeepAnchor);
    QTextCharFormat tcf = cursor.charFormat();
    QBrush brush = tcf.foreground();
    brush.setColor(Qt::blue);
    tcf.setForeground(brush);
    cursor.setCharFormat(tcf);
    }
    The text is well selected, the brush is now blue but the text in the block remains black. What did I forget to do?
    Thank you for your kind answer

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

      Hi,

      What if you replace:
      @Olivier-Ronat said in How to change the colour of a QTextBlock in a QTextEdit:

      QBrush brush = tcf.foreground();
      brush.setColor(Qt::blue);
      tcf.setForeground(brush);

      with just:

      tcf.setForeground(Qt::blue);
      

      ?

      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
      • O Offline
        O Offline
        Olivier Ronat
        wrote on last edited by
        #3

        @SGaist
        Nothing is changed

        1 Reply Last reply
        0
        • O Offline
          O Offline
          Olivier Ronat
          wrote on last edited by
          #4

          Found the issue.
          It is not a QT one but the block parameter in the calling function was coming from a const QTextEdit so it can change anything in it. Yhank you for the time you spent to answer me.
          Olivier

          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