python-3.x - PyQt5 QTextEdit changes color when copy / pasting into it
问题描述
In my application, I have a QTextEdit. It works just fine when I write normally into it but when I copy / paste text from my IDE ( as an example - in my case, pycharm in dark mode ) into it, the QTextEdit also takes the color and background of the text.
This is the normal appearance :
This is what happens when I copy paste from my IDE :
When the color is changed, the next writing inputs will keep the same colors until the next copy / paste.
How can I avoid the QTextEdit having anything else than the default colors ( black text, white background ) ?
解决方案
QTextEdit has the acceptRichText
property.
Just set it to False.
QTextEdit allows using rich text content, and if the source you're getting the text from supports rich text for the clipboard, you'll get that.
To avoid this behavior, you can subclass QTextEdit and override insertFromMimeData(mimeData)
class TextEdit(QtWidgets.QTextEdit):
def insertFromMimeData(self, source):
newData = QtCore.QMimeData()
for format in source.formats():
if format == 'text/plain':
newData.setData(format, source.data(format))
super().insertFromMimeData(newData)
推荐阅读
- github - 如何获取 github 项目的 Social preview?
- methods - 是否可以将元组解包到方法参数中?
- python-3.x - 从 macOS 卸载 python 3
- javascript - 来自本地服务器的 GET 请求不起作用
- angular - Angular 模块路由 - 不是路由
- java - 使用自动分页符打印 WebView/WebEngine
- c++ - 如果带有模板参数和参数包,如何使用启用?
- azure - 使用用户分配的托管标识进行 Azure 服务总线身份验证
- javascript - 异步函数存储另一个异步函数的数据
- rest - 使用机器人框架和 REST 上传带有 Post 请求的 zip 文件