Qt (and therefore PyQt) allows you to add a QSS (Qt Style Sheet) file to a QApplication, and one to each individual widget you create. You can set your application's style sheet with the following Python/PyQt code:
# app is a QApplication and styleSheet is a str.
app.setStyleSheet(styleSheet)
However, the setStyleSheet() function only allows you to specify one QSS file for the QApplication. This is unlike CSS, which allows any number of style sheets to be applied to a single webpage.
I'm writing a program in PyQt which will allow user-customizable themes. The program has a main QSS file, and themes should be able to contain additional QSS which would be cascaded on top of it. However, due to this apparent Qt limitation, this doesn't seem possible.
One idea I've thought of is that I could assign the program's default style sheet to the QApplication, and the current theme's style sheet to the QMainWindow. This seems like a workaround rather than a good idea, though.
Is there a good way to add two Qt Style Sheets like this?
You can do:
app.setStyleSheet(stylesheet1 + stylesheet2)
This will work the same as normal HTML, where the stylesheets are simply concatenated together in the order in which they appear in the document. After that, the standard rules of CSS specificity apply.
Related
I have css class which needs to be added to three different component(for example) which might not require for other components of our application.
which one would be the best approach.
add that css class to style.css (global css)and use it or
add it to three different component specific style sheet as it is not used anywhere in the application(is this considered as code duplicate ?)
Thanks!
I would say that adding it to the global styles is just fine for this purpose. View encapsulation is cool, but the cascading part of CSS is still something that we're supposed to take advantage of...just as long as you're still cognizant of keeping styles organized and not too high of specificity.
Conversely, if you knew all three components would share parent component, you could turn off view encapsulation for that component and add the class there, which is essentially the same as adding to global styles with the difference being the style would only be loaded when the component is loaded.
You could also use ::ng-deepon a parent component to target its children. Sass brings other solutions, but it doesn't look like you're using .scss files.
I have a QTextEdit where I do display some HTML. Could I apply a stylesheet to that very HTML content?
Do not confuse it with applying a Qt stylesheet to the QTextEdit (that I know). I want to change the appearance of what is in the QTextEdit widget ("HTML").
--- edit ----
Related: Default HTML style for controls in the Qt library
You can have a look at Qt ignores CSS in QTextDocument which uses QTextDocument and the defaultStyleSheet property / setDefaultStyleSheet() http://doc.qt.io/qt-5/qtextdocument.html#defaultStyleSheet-prop
Note that QTextEdit only supports the following html subset http://doc.qt.io/qt-5/richtext-html-subset.html if you want to do more then that you are probably best of using the QWebkit or QWebEngine modules depending on which Qt version your targeting.
As per Qt5, many CSS properties are supported:
The following table lists the CSS properties supported by Qt's rich text engine.
The best way to apply them is to start your document with <style> and use classes. However the <node style=""> attribute also works.
I have a global stylesheet selector (the '*' block) which changes font properties for all Qt widgets in the application. However, that also affects QToolTip - not desired. How do you exclude QToolTip from being affected by the global selector? I couldn't find a stylesheet operator for exclusion.
I can of course not use the global selector and copy the shared styles for every possible widget type. Obviously this is cumbersome, not scalable, clutters the QSS and may not provide full coverage. I'm looking for a better solution.
Add this line:
QToolTip{}
Should reset the QToolTip style to default.
QToolTip uses font from widget. You apply another font to widget so this font applies also to tooltip. To avoid this try to use html tooltip:
ui->lineEdit->setToolTip("<font face =\"aharoni\">test</font>");
ui->lineEdit->setText("test");
Result with aharoni font (for better visual comparison):
with *{font-family:consolas} stylesheet.
I'm developing a multi-module application using GWT 2.5.1. I'm not using any GWT theme. I want to customize the style for some of the GWT widgets, for example Button and CheckBox.
I see two solutions:
Write a CSS file loaded in the application (link in the HTML page). The CSS will contain CSS rules using GWT defined names, like .gwt-Button for buttons and .gwt-CheckBox, .gwt-CheckBox-disabled for checkboxes. This solution don't takes the advantage of CSS optimizations made by the GWT compiler.
Use a CssResource and set the style name each time I use a Button or a Checkbox. This solution will take advantage of CSS optimizations but it requires to set the style name every time I create a new Widget.
There are other solutions? Which is the correct one?
You can put those styles in a CssResource as well.
Just put #external on top of those styles in your css file, and you are good to go.
For example:
#external gwt-DatePicker;
.gwt-DatePicker {
...
}
Hope it helps.
Other solution: Button is html element button and Checkbox an html element input[type=checkbox]. So you could set styles on those elements and use css selectors for specific states. i.e. button:disabled. That way you won't have to set style names, or don't have lots of extra style names and use cleaner css.
You could subclass whatever widgets you want to style (e.g. MyButton), and have your subclass either just add a style name to each widget that gets created, or do the styling inline using calls to this.setWidth(), this.getElement().getStyle.setXXX.
Also, what optimizations does the GWT compiler perform on CSS? I know that it will obfuscate style names to avoid collisions, but I'm not sure CSS is even able to be optimized?
I would personally use emanuele's solution, but just to offer an alternative: you can use a widget's getElement() method to access style names directly, so if you really want to, you can override the style names with ones you created. This gets rather difficult, however, with larger widgets and panels that have multiple styles.
I am designing a website, and am going to be implementing a sort of say "Wiki". I am not doing a script, none of that, it's going to be pure XHTML and CSS.
What I want to know from the StackOverflow community, is how I should approach this scenario.
I want to be able to pull out an external stylesheet, in which is lightweight, while maintaining the style sheet for the overall design (because, like I said, it's being implemented).
So instead of basically copying and pasting the style sheet used from the entire design, I want to be able to call and external style sheet, in which I can call for the specific divs used in the Mini Wiki.
I want to know how I can call that said style sheet, before the divs that are going to be called in the HTML document, so the following divs can have the custom styles applied through the specified external style sheet for the Mini Wiki.
Is that possible? Is it possible to call an external style sheet in a div, allowing the styles to override the default style sheet of the page? I am confused and would love some help. I want some feedback and some ideas.
No, this isn't possible.
A stylesheet can only be applied to an entire document.
You can limit a rule-set to a section of the document by using a descendant combinator.