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

Wrong QTextCharFormat got from a given position by contentsChange signal of a QTextDocument



  • Hello,
    let's say I have a QTextDocument with 5 characters, I manually select the character at position 3 using a QTextEdit and I apply a fontWeight invoking setFontWeight. Inside a slot connected to the contentsChange signal of the document I create a QTextCursor initializing it with textCursor() and I fetch the QTextCharFormat at the position above (3), it does not contain the applied font weight.


  • Lifetime Qt Champion

    @anphetamina In https://doc.qt.io/qt-5/qtextdocument.html#contentsChange
    there is "The signal is emitted before the document's layout manager is notified about the change. This hook allows you to implement syntax highlighting for the document.". So, maybe the change is not yet applied when the signal is emitted? Did you check where the cursor actually is (is it at correct position?)?



  • I place the cursor at the position emitted by the signal contentsChange

    "maybe the change is not yet applied when the signal is emitted? "

    No, because I'm able to fetch the correct format using currentCharFormat(), but it works only when the textCursor() position is right after the position in which the character has been placed. Let's say I made the selection from right to the left, currentCharFormat() picks the format of the previous character.


Log in to reply