如何在QTextEdit中设置自定义文本颜色?

发布于 2024-11-02 21:51:01 字数 1483 浏览 1 评论 0原文

我一直拒绝问这个看似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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2024-11-09 21:51:01

我已经通过更改调色板成功更改了 QLabelQPlainTextEdit 的文本颜色:

QPalette pal;
pal.setColor(QPalette::Window, bgColor);
pal.setColor(QPalette::WindowText, fgColor);
pal.setColor(QPalette::Text, fgColor);
mylabel->setPalette(pal);

I've had success changing text color of QLabel and QPlainTextEdit by changing the Palette:

QPalette pal;
pal.setColor(QPalette::Window, bgColor);
pal.setColor(QPalette::WindowText, fgColor);
pal.setColor(QPalette::Text, fgColor);
mylabel->setPalette(pal);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文