Qt库中控件的默认HTML样式 [英] Default HTML style for controls in the Qt library

查看:517
本文介绍了Qt库中控件的默认HTML样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于Qt库的问题,而不是关于网页设计。



对于QLabel和其他控件,我可以设置HTML文本,例如< h3> Some文本< / h3>。问题是:在哪里定义了默认的HTML样式?下一个问题:我可以更改默认的HTML样式吗?

$

b
$ b

编辑:我想在我的代码中在一个地方指定我的控件将如何显示。在所有标签中指定css样式对我来说似乎不是一个优雅的解决方案。



Edit2:似乎人们没有得到这个问题。我会再试一次。假设我执行以下操作:

 

QLabel * label = ...
- > setText(This< b> is< / b> a< h3> Header< / h3>);

问题:什么字体将用于标签文本呈现?如何控制它们?是否有一种方法可以指定

头的默认字体大小?



Edit3:Thomi建议使用QTextDocument :: setDefaultStyleSheet。但这只是一个解决方法。我必须手动应用css样式到所有的QTextEdits(而不是QLabels)在界面。问题是:如何找出默认样式表? QTextDocument :: setDefaultStyleSheet刚刚覆盖它为单个QTextDocument对象。也许QTextDocument :: defaultStyleSheet返回吗?我现在没有一台有Qt insatlled的电脑,所以我不能检查它。

解决方案

做一个QLabel。 QLabel设计为保存原始文本标签 - 它的HTML支持是... ropey。



但是,您可以 QTextEdit& QTextDocument。



尝试这样(我从内存中写入,所以它可能无法编译或100%正确):

  QTextDocument * doc = new QTextDocument(this); 
doc-> setDefaultStyleSheet(h3 {font-color:red;});
QTextEdit * edit = new QTextEdit(this);
edit-> setDocument(doc);
edit-> setHTML(这是红色< h3>标题< / h3>);

重要的是使用QTextDocument,它允许你改变HTML样式表。从QT文档:

 
默认样式表应用于插入到文档中的所有新的HTML格式的文本,例如使用setHtml ()或QTextCursor :: insertHtml()。

样式表需要符合CSS 2.1语法。

注意:更改默认样式表对文档的现有内容没有任何影响。

详情请参阅这里



编辑:



要获取默认样式表,你可以调用 QTextDocument :: DefaultStyleSheet() - 然而,这只适用于QTextDocuments,可能或可能不适用于所有Qt控件(包括QLabel)。


This is a question about Qt library, not about Web design.

For QLabel and other controls I can set HTML text, for example "<h3>Some Text</h3>". The question is: where is the default HTML style is defined? How can I find out what a font would be used for <h3> tag?

The next question: can I change the default HTML style?

Edit: I want to specify in one place in my code how my controls would be displayed. To specify css style in all the labels doesn't seem to be an elegant solution to me.

Edit2: It seems people don't get the question. I'll try again. Suppose I do the following:


QLabel* label = ...
label->setText("This <b>is</b> a <h3>Header</h3>");

The question: what fonts will be used for label text rendering? How can I control them? Is there a way to specify, say, default font size for <h3> headers?

Edit3: Thomi have suggested to use QTextDocument::setDefaultStyleSheet. But this is just a workaround. I have to manually apply css style to all the QTextEdits (and not QLabels) in the interface. And the question was: how to find out the default style sheet? QTextDocument::setDefaultStyleSheet just overrides it for a single QTextDocument object. Maybe QTextDocument::defaultStyleSheet returns it? I don't have a computer with Qt insatlled right now so I can't check it.

解决方案

What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.

However, You can achieve this using a QTextEdit & QTextDocument.

Try something like this (I'm writing this from memory, so it may not compile or be 100% correct):

QTextDocument *doc = new QTextDocument(this);
doc->setDefaultStyleSheet("h3 { font-color: red; }");
QTextEdit *edit = new QTextEdit(this);
edit->setDocument(doc);
edit->setHTML("this is a red <h3>heading</h3>");

The important thing is to use a QTextDocument, which allows you to change the HTML stylesheet. From the QT documentation:

The default style sheet is applied to all newly HTML formatted text that is inserted into the document, for example using setHtml() or QTextCursor::insertHtml().

The style sheet needs to be compliant to CSS 2.1 syntax.

Note: Changing the default style sheet does not have any effect to the existing content of the document.

see here for more info

Edit:

To get the default stylesheet, you can call QTextDocument::DefaultStyleSheet() - however, this only applies to QTextDocuments, and may or may not apply to all Qt controls (including QLabel).

这篇关于Qt库中控件的默认HTML样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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