Default HTML style for controls in the Qt library - c++

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

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.

Have a look at Qt Documentation about Style Sheets
You can probably use QApplication::setStyleSheet() or QWidget::setStyleSheet() to get it done.

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

Related

Removing the title from a QGroupBox

Is it possible to completely remove the title from a QGroupBox? If you just give it an empty title, the label where the title would be still takes up space. It looks like this:
But I want it to look like this instead:
I tried the following things without success:
Setting the title font size to zero
Giving the title a size of zero in the stylesheet via setStyleSheet("QGroupBox:title{ max-width: 0; max-height: 0; }");
Moving the title inside the box via setStyleSheet("QGroupBox:title{ subcontrol-position: center center;}")
Note: You might say that a group box without title is a use case for QFrame. The problem with this is that I want to mix groups/frames with and without title, but the frame has a different visual style than the group box. So if you could tell me how to make QFrame look like QGroupBox that would solve my problems too.
You could use:
setStyleSheet("QGroupBox{padding-top:15px; margin-top:-15px}")
What you see is a bug of the particular style you're using. This pretty much "works" without you having to do anything special on other common styles (e.g. Mac, Windows). If you insist on using the particular style in question, you'll have to patch it yourself and either copy the style into your project, or build entire Qt to have a fixed version.

Qt global css stylesheet?

In Qt, it is possible to use a subset of html and css to style (for example) QLabels:
QLabel label = new QLabel("This link will be colored: "<a style='color:#00aeef;' href='http://www.example.com'>link</a>.");
My question:
Is it possible to use a global stylesheet that makes all links in all QLabels colored or do I have to add the inline style to every link in every label?
Note
As far as I know, the style I want to change cannot be modified (as suggested in this question) with QApplication::setStyleSheet. I want to change the style of embedded HTML markup, but setStyleSheet only modifies styles of QT Objects (no "a href" element anywhere on this page).

Set Qt tooltip area constraints programmatically?

I have a QWidget that has a tooltip set. The issue is, it's displayed on one huge line(the tooltip is quite large). I would like to put a constraint on the width of the tooltip, so it would be displayed on multiple smaller lines instead of a huge one. Is there a way to set this programmatically, in the C++ code?
Qt tool tip is HTML aware. You can design the tool tip using HTML tags
Here is a simple example
ui->pushButton->setToolTip(QString("<div style = 'background-color:yellow;float:left'> <p><b>test tool tip</p></div>"
"<div> <img src=':/someImage.png'></div>"
"<div style = 'background-color:red;float:left'> <p><b>test tool tip</p></div>"));

Setting CSS style through web pages and QWidgets

I'm creating a Qt application with some web pages loaded on QWebViews and also some plain QWidgets, all inside a QMainWindow. Both the webpages and widgets are going to have some checkboxes here and there, and I'd like them to look the same. Well, at first they DO look the same, because they inherit their style from the native theme engine, but I'd like to define a new CSS style for both.
I tried some things like
QCheckBox::indicator:unchecked {
image: url(:/image/checkbox_unchecked.png);
}
but that only works for checkboxes inside my QWidgets.
I also tried some plain CSS style, but then they only apply to webpage checkboxes.
So, is there a way I could design a single CSS style for checkboxes and apply it to all sorts of checkboxes inside my application?
For the record, I could achieve exactly that when it comes to text selection. I set the main window stylesheet to
*{
selection-color: #363636; selection-background-color: #bfebeb;
}
and both web page and qwidget selections followed this style.
UPDATED
change your code to
QCheckBox:!checked {
image: url(:/image/checkbox_unchecked.png);
}
QCheckBox:checked {
image: url(:/image/checkbox_unchecked.png);
}
http://harmattan-dev.nokia.com/docs/library/html/qt4/stylesheet-syntax.html

QTextEdit how to modify stylesheet elements from C++

I'm looking for the fastest way to modify css style for body element inside QTextEdit instance.
I tried this but it doesn't work
ui->textEdit->setStyleSheet("body {background-color: #aaa}");
I need a method that would update internal css stylesheet of the html document open inside QTextEdit.
setStyleSheet updates the style sheet of the widget itself, not of the HTML document which is displayed by the text edit. As a QTextEdit does not have a "body" part, the line is ignored.
If you want the HTML document to use a different style sheet, you would have to modify the displayed HTML content (or rather the hidden stylesheet include line) directly
You are using the correct member function QWidget::setStylesheet() if you need examples on how to apply the proper Stylsheet, Qt has some examples here