如何在QTextEdit中设置自定义文本颜色?
我一直拒绝问这个看似n00b的问题,但我最近为完成这项任务所做的所有努力都失败了。这是我已经尝试过的事情,但都失败了!难道我的 OpenSuse 11.3 设置了系统范围的样式设置,默认情况下甚至适用于我的 Qt 应用程序?
//I have some QTextEdit created in QDesigner -- call it myQEdit
QString str = "some content i want to display"
//trial one:
QString my_html_template = "<html><head></head><body style=\"color:__color__;\">__content__</body></html>"
myQEdit->document()->setHtml(my_html_template.replace("__color__","#99ff00").replace("__content__",str));
失败了,然后我尝试...
//trial two:
myQEdit->setDocument(new QTextDocument(str,this));
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00;}");
我什至尝试在我传递的 color
值上设置 !important
css 标志:
但这也失败了!
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00 !important;}");
因此,我决定从设计器本身设置 QTextEdit
的颜色 - 通过在选项中指定我的自定义颜色来设置 QTextEdit
的原始 html 内容。虽然我没有以编程方式更改内容,但使用了所需的颜色。但是当我像这样设置自定义内容时:
myQEdit->setDocument(new QTextDocument(str));
我失去了在 QTextEdit
上从 QDesigner
设置的颜色设置。那么实现我的愿望的正确方法是什么?我知道可以通过某种方式完成...
最后: 使用下面接受的答案中的提示后,我是如何解决这个问题的:
myQEdit->setDocument(new QTextDocument(str,this));
QPalette pal;
pal.setColor(QPalette::Text, QColor::fromRgb(0,150,0));
myQEdit->setPalette(pal);
I have resisted asking this seemingly n00b question, but all my recent effort to achieve this task have failed. Here are the things I have tried already, all have failed! Could it be that my OpenSuse 11.3 sets system-wide style settings that apply to even my Qt app by default?
//I have some QTextEdit created in QDesigner -- call it myQEdit
QString str = "some content i want to display"
//trial one:
QString my_html_template = "<html><head></head><body style=\"color:__color__;\">__content__</body></html>"
myQEdit->document()->setHtml(my_html_template.replace("__color__","#99ff00").replace("__content__",str));
that fails, then i tried...
//trial two:
myQEdit->setDocument(new QTextDocument(str,this));
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00;}");
I even tried setting the !important
css flag on the color
value i pass like:
but this failed too!
myQEdit->document()->setDefaultStyleSheet(" body { color:#99ff00 !important;}");
So I decided to set the color of my QTextEdit
from the designer itself - by specifying my custom color in the option to set the raw html content of the QTextEdit
. While i didnt change the content programmatically, the desired color was used. But the moment I set custom content like this:
myQEdit->setDocument(new QTextDocument(str));
I loose the color settings I'd set from QDesigner
on the QTextEdit
. So what is the correct way to achieve what am desiring? I know it can be done some way...
Finally:
After using the hint from the accepted answer below, here is how I've solved it:
myQEdit->setDocument(new QTextDocument(str,this));
QPalette pal;
pal.setColor(QPalette::Text, QColor::fromRgb(0,150,0));
myQEdit->setPalette(pal);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经通过更改调色板成功更改了
QLabel
和QPlainTextEdit
的文本颜色:I've had success changing text color of
QLabel
andQPlainTextEdit
by changing the Palette: