1
votes

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.

4
Sergy - I didn't catch the distinction since it has been a very long time since I've read anything about Qt. Thus, I've deleted my answer. Thanks for clarifying.Mark Brittingham
Sergey - I think your question title might need amending to mention Qt / C++Jon Winstanley

4 Answers

4
votes

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).

2
votes

As mentioned in other answers you can do it for widget styles but not for HTML tags. The only way to do it is to set CSS styles inside your widgets text property individually.

Qt uses its rich text engine to render HTML tags which are defined according to rules in HTML 4 specification. See Supported HTML Subset

If you need just one style for all your labels why not to set it using setStyleSheet() like this:

MainWindow w;    
w.setStyleSheet("QLabel{font-size:20px; color:purple;};");

Unless you want to use more than one style inside your labels (for example: "More than one style") that's the right way to do it.

1
votes

Have a look at Qt Documentation about Style Sheets

You can probably use QApplication::setStyleSheet() or QWidget::setStyleSheet() to get it done.

0
votes

In latest QT ie on QT4..The above answers are working. Tell which QT version u r working on... Try the following...

 QLabel{
     border: 2px solid green;
     border-radius: 4px;
     padding: 2px;
     background-image: url(images/welcome.png);
 }

link