You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QTextEdit末尾空格背景色不显示问题排查

QTextEdit插入带背景色字符串时末尾空格背景色不显示的问题

使用QTextEdit插入带背景色的字符串时,发现末尾空格无法显示背景色。我已编写了最小可复现示例(MCVE),不过重写代码后问题消失。

编辑补充:当窗口最大化时,空格的背景色会正常显示,我会继续排查原因。

最小可复现示例

from PySide2 import QtWidgets, QtGui, QtCore

class Terminal(QtWidgets.QTextEdit):
    def __init__(self, handler, width=80, height=30):
        super().__init__()

        self.setStyleSheet("Terminal {background-color: black;" 
                                     "color: white;" 
                                     "font: 10pt Consolas}")
        self.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)

        self.setPlainText("\n".join([" " * width] * height))
        
        self._width = width
        self._cursor = self.textCursor()

        self._handler = handler(self)
        next(self._handler)

    def say(self, row, col, text, **kwargs):
        format = QtGui.QTextCharFormat()
        
        for key, value in kwargs.items():
            getattr(format, f"set{key}")(value)

        start = row * (self._width + 1) + col

        self._cursor.setPosition(start)
        self._cursor.setPosition(start + len(text), QtGui.QTextCursor.KeepAnchor)

        self._cursor.insertText(text, format)

app = QtWidgets.QApplication([])

@Terminal
def widget(win):
    win.say(0, 0, "hello  ", Background=QtGui.QColor("red"))
    yield

widget.show()

app.exec_()

请问我可能哪里操作出错了?


内容的提问来源于stack exchange,提问作者Dante S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 06:15:02