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

Can't distinguish between white and black with `QTextEdit.textBackgroundColor()`



  • I'm trying to implement text highlighting with QTextEdit, but the widget sometimes interprets white background color as black.

    >>> from PySide6.QtCore import Qt
    >>> from PySide6.QtGui import QColor
    >>> from PySide6.QtWidgets import QApplication, QTextEdit
    >>> app = QApplication([])
    >>> text_edit = QTextEdit()
    >>> QColor(Qt.black) == text_edit.textBackgroundColor()
    True
    

    I can't just manually set the background color, because pasting external text into the QTextEdit will make textBackgroundColor() return black when the actual background color is white. Is this a bug? Is there something I'm missing?

    Here's a more complete example of my problem:

    import copy
    
    from PySide6.QtCore import Qt
    from PySide6.QtGui import QColor
    from PySide6.QtWidgets import QApplication, QTextCursor, QTextEdit
    
    if __name__ == "__main__":
        app = QApplication([])
        text_edit = QTextEdit()
    
        initial_white_background_color = copy.deepcopy(text_edit.textBackgroundColor())
    "this color displays white, but is equal to black in Python code"
    
        text_edit.setText("Lorem ipsum dolor")
    
        # Highlight second word yellow
        cursor = text_edit.textCursor()
        cursor.movePosition(QTextCursor.NextWord)  # skip first word
        cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveMode.KeepAnchor)
        text_edit.setTextCursor(cursor)
        text_edit.setTextBackgroundColor(Qt.yellow)
    
        # Make the second word have white font and black background
        cursor.clearSelection()
        cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveMode.KeepAnchor)
        text_edit.setTextCursor(cursor)
        text_edit.setTextColor(Qt.white)
        text_edit.setTextBackgroundColor(Qt.black)
    
        text_edit.show()
        sys.exit(app.exec())
    

    The above results in the following window
    c14c7350-5263-4695-807f-2b96fb71b2d7-image.png
    Note that programatically textBackgroundColor() says the background of the "Lorem" text is black, but displays it as white. The other words render as expected.

    To further demonstrate the problem, let's change the entire document's background color to the initial background of QTextEdit

    app = QApplication([])
        text_edit = QTextEdit()
        initial_white_background_color = copy.deepcopy(text_edit.textBackgroundColor())
        text_edit.setText("Lorem ipsum dolor")
    
        # Highlight second word yellow
        cursor = text_edit.textCursor()
        cursor.movePosition(QTextCursor.NextWord)  # skip first word
        cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveMode.KeepAnchor)
        text_edit.setTextCursor(cursor)
        text_edit.setTextBackgroundColor(Qt.yellow)
    
        # Make the second word have white font and black background
        cursor.clearSelection()
        cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveMode.KeepAnchor)
        text_edit.setTextCursor(cursor)
        text_edit.setTextColor(Qt.white)
        text_edit.setTextBackgroundColor(Qt.black)
    
        # Reset entire document to initial "white" background color
        cursor.clearSelection()
        cursor.movePosition(QTextCursor.Start)
        cursor.movePosition(QTextCursor.End, QTextCursor.MoveMode.KeepAnchor)
        text_edit.setTextCursor(cursor)
        text_edit.setTextBackgroundColor(initial_white_background_color)
    
        text_edit.show()
        sys.exit(app.exec())
    

    You'd expect all the text to have a white background right? Wrong!
    675bd243-0174-4454-bcb4-0cc0cc931c9e-image.png
    The color that originally rendered the background behind "Lorem" as white is actually black!

    To be clear my problem is that when I recieve "black" from QTextEdit.textBackgroundColor() I don't know if it is truly black, or is actually white. Please tell me there's a workaround!


  • Lifetime Qt Champion

    Hi,

    What about using QSyntaxHighlighter ?


Log in to reply