tags:

views:

1042

answers:

4

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.

+1  A: 

Have a look at Qt Documentation about Style Sheets

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

Raf
Edited, that should be the right answer.
Raf
I've read it before creating the question. There is not a word about HTML.
Sergey Skoblikov
no. He's asking about HTML stylesheets, which are not the same thing as widget stylesheets. If I understand correctly he's looking to change the HTML style that QLabel uses to render simple HTML text, rather than change the way the label widget looks on screen.
Thomi
+1  A: 

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.

Raf
I need to use more than one style. I guess I'll create some ugly wrapper that will add styles to html labels.
Sergey Skoblikov
A: 

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

cdb
The above answers are working but for different questions I suppose. I edit the question in attempt to clarify it.
Sergey Skoblikov
+1  A: 

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

Thomi
I've reflected your answer in the question.
Sergey Skoblikov
It seems there is no other way to deal with the problem. I've accepted you question.
Sergey Skoblikov