a textedit text color problem
-
When I append text1 with green color, and select the green text, then append text2 without <font> label, but text2 become green too:
Step I: click "add text green color"
Step II: select "green text"
Step III: click "add text unset color"
If I don't select the green text1 to append text2, text2 will be white (or black in light theme):
Step I: click "add text green color"
Step II: click "add text unset color"
PySide6(Qt6 for Python): 6.7.2
My Code:import sys from PySide6 import QtWidgets app = QtWidgets.QApplication(sys.argv) win = QtWidgets.QMainWindow() central = QtWidgets.QWidget(win) win.setCentralWidget(central) layout = QtWidgets.QVBoxLayout(central) central.setLayout(layout) text_edit = QtWidgets.QTextEdit(central) layout.addWidget(text_edit) add_normal_text_btn = QtWidgets.QPushButton(central) add_normal_text_btn.setText("add text unset color") add_normal_text_btn.clicked.connect(lambda: text_edit.append("normal text")) layout.addWidget(add_normal_text_btn) add_green_text_btn = QtWidgets.QPushButton(central) add_green_text_btn.setText("add text green color") add_green_text_btn.clicked.connect(lambda: text_edit.append("<font color=\"green\">green text</font>")) layout.addWidget(add_green_text_btn) win.show() sys.exit(app.exec())
-
When I append text1 with green color, and select the green text, then append text2 without <font> label, but text2 become green too:
Step I: click "add text green color"
Step II: select "green text"
Step III: click "add text unset color"
If I don't select the green text1 to append text2, text2 will be white (or black in light theme):
Step I: click "add text green color"
Step II: click "add text unset color"
PySide6(Qt6 for Python): 6.7.2
My Code:import sys from PySide6 import QtWidgets app = QtWidgets.QApplication(sys.argv) win = QtWidgets.QMainWindow() central = QtWidgets.QWidget(win) win.setCentralWidget(central) layout = QtWidgets.QVBoxLayout(central) central.setLayout(layout) text_edit = QtWidgets.QTextEdit(central) layout.addWidget(text_edit) add_normal_text_btn = QtWidgets.QPushButton(central) add_normal_text_btn.setText("add text unset color") add_normal_text_btn.clicked.connect(lambda: text_edit.append("normal text")) layout.addWidget(add_normal_text_btn) add_green_text_btn = QtWidgets.QPushButton(central) add_green_text_btn.setText("add text green color") add_green_text_btn.clicked.connect(lambda: text_edit.append("<font color=\"green\">green text</font>")) layout.addWidget(add_green_text_btn) win.show() sys.exit(app.exec())
@lfmissonb
I can only guess/suggest that when you append your "unset color text" (which btw does not unset any color, perhaps it should) theQTextEdit
/QTextDocument
makes a guess whether to continue from existing selected text or not. So you get what you get. I suspect the Qt folks will say this is "by design" or "unspecified", so you will need to experiment with what works/does not work when it updates the HTML to get what you want. -
@lfmissonb
I can only guess/suggest that when you append your "unset color text" (which btw does not unset any color, perhaps it should) theQTextEdit
/QTextDocument
makes a guess whether to continue from existing selected text or not. So you get what you get. I suspect the Qt folks will say this is "by design" or "unspecified", so you will need to experiment with what works/does not work when it updates the HTML to get what you want. -
-
@JonB
Thank you. I found that I shouldn't only append "normal text", I should append "<font>normal text</font>" instead, and the "normal text" will not be green.@lfmissonb Indeed, because that specifies its own font (and so color) for the fragment you are adding.