Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to change the colour of a QTextBlock in a QTextEdit



  • 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


  • Lifetime Qt Champion

    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);
    

    ?



  • @SGaist
    Nothing is changed



  • 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


Log in to reply