在QTextEdit中更改单个字符串的颜色 [英] Changing a single strings color within a QTextEdit

查看:699
本文介绍了在QTextEdit中更改单个字符串的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究通过PyQt和Qt4开发的GUI.在我的GUI中,我有一个QTextEdit,其中写入了各种数据.有没有一种方法可以操纵QTextEdit中一个单词的颜色?

I am working on a GUI developed via PyQt and Qt4. Within my GUI I have a QTextEdit that has various data written to. Is there a way in which I can manipulate the color of one word within the QTextEdit?

例如

redText = "I want this text red"
self.myTextEdit.write(redText)
blackText = "And this text black"
self.myTextEdit.append(blackText)

这可能吗?如果是这样,我该怎么办?

Is this possible? If so, how could I do this?

此致

sudo !!

推荐答案

您应该为其提供富文本格式.可以通过创建<span>标签并将color属性设置为RGB值来完成:

You should provide a rich text for it. It can be done by creating a <span> tag and setting the color property to an RGB value :

redText = "<span style=\" font-size:8pt; font-weight:600; color:#ff0000;\" >"
redText.append("I want this text red")
redText.append("</span>")
self.myTextEdit.write(redText)

blackText = "<span style=\" font-size:8pt; font-weight:600; color:#000000;\" >"
blackText.append("And this text black")
blackText.append("</span>")
self.myTextEdit.append(blackText)

这篇关于在QTextEdit中更改单个字符串的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆