QTextEdit: textBackgroundColor returns black if white
Unsolved
Qt for Python
-
Hi,
Please provide:
- Version of PySide/PyQt
- Version of Python
- Method of installation
- OS your are running
- Minimal script that reproduce that behaviour
-
PySide6 installed by pip
Pyside6 version: 6.4.1
Python version: 3.11.0
OS: Windows 11Reproducer:
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit from PySide6.QtCore import QRect import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text_entry = QTextEdit(self) self.text_entry.setGeometry(QRect(0, 0, 917, 670)) self.text_entry.setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\nhr { height: 1px; border-width: 0; }\nli.unchecked::marker { content: \"\\2610\"; }\nli.checked::marker { content: \"\\2612\"; }\n</style></head><body style=\" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#ffffff; background-color:#000000;\">I'm black</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif';\">I'm white</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#6cdfff; background-color:#ff8649;\">I'm colorful</span></p></body></html>") self.text_entry.selectionChanged.connect(self.on_selection_changed) def on_selection_changed(self): print(self.text_entry.textBackgroundColor().name()) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
-
It's not a bug, you've actually never set the color or the background color for the white button. I've updated your code to add the background color and the color for the white button:
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit from PySide6.QtCore import QRect import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text_entry = QTextEdit(self) self.text_entry.setGeometry(QRect(0, 0, 917, 670)) self.text_entry.setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\nhr { height: 1px; border-width: 0; }\nli.unchecked::marker { content: \"\\2610\"; }\nli.checked::marker { content: \"\\2612\"; }\n</style></head><body style=\" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#ffffff; background-color:#000000;\">I'm black</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#000000; background-color:#ffffff;\">I'm white</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'MS Sans Serif'; color:#6cdfff; background-color:#ff8649;\">I'm colorful</span></p></body></html>") self.text_entry.selectionChanged.connect(self.on_selection_changed) def on_selection_changed(self): print(self.text_entry.textBackgroundColor().name()) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
This updated code works perfectly on my end.
In case you're wondering what I've done, I've just added:
color:#000000; background-color:#ffffff;
Before the code:
\">I'm white</span></p>\n<p style=\