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
Related
Is there a simple way to obtain inherited stylesheet for a widget, if stylesheets were set in its parents (some levels above)? stylesheet() would return local value of property only.
In theory, to obtain text of effective stylesheet, I have to walk up to the topmost parent, then concatenate found styles, in order of parenthood. I was wondering if there is simpler way, library ALREADY does same thing. Problem is that concatenation doesn't work right with local stylesheets that do not have a selector in them
QWidget::styleSheet() call would return only the text for particular widget if set. Stylesheet is result of syntax parsing of that text, do I need to create my own parser of style sheet, that have to recreate way , how Qt work?
The solution indeed is either to create a parser and a generator, or use so an external source to generate style sheets. I ended up with an xml parser which was creating style sheet definitions for all widgets from a single configuration file, which was simpler to do and less error-prone.
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).
I would like to format my clickable URLs inside a QTextBrowser. In a QSS file that I set for the entire application, I've tried QTextBrowser::text.a and QTextBrowser.a, but neither worked. Am I actually expected to manually format every link using the Qt Creator editor, or is there a way around this?
You can’t use QSS for that, but QTextBrowser supports a subset of CSS, so you can make it part of the displayed HTML, or set a default stylesheet: QTextDocument::defaultStyleSheet.
What is exactly richtext and how do I format data in richtext in QTextEdit?
The internal rich text format is tag/attribute-based, and is similar to HTML using in-line CSS style="xxx" attributes. The default export/import format (using toHTML/setHTML) is a subset of HTML. See this link:
http://doc.trolltech.com/4.6/richtext-html-subset.html
Note that CSS classes are not supported internally and are converted to their representing attributes at import.
QTextEdit can display text that is formatted with additional layout and style information (i.e. enriched) which can be set either programatically or by using a subset of HTML.
The Rich Text Processing documentation gives you an overview and pointers to more in-depth information.
If you're looking for a WYSIWYG toolbar for the QTextEdit, you'll end up disappointed. There is no default one (although the SDK contains an example app). So you should either make the toolbar yourself or find one.
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