I need to change between two styles in my app: custom, stylesheet and default one. I tried to change it by:
qApp->setStyle(new QCleanlooksStyle);
but it doesn't change the colors, images, etc.
You can invoke setStyleSheet with an empty string.
qApp->setStyleSheet( "" );
Setting the style and setting the stylesheet are two different things.
If you have an application-wide stylesheet, that is, you called QApplication::setStyleSheet(), then you'll need to clear it for your setStyle() function to really have any effect:
qApp->setStyleSheet( QString() );
If, on the other hand, you have random bits of style set on your individual widgets, then you'll probably have to migrate to a global style sheet first.
Related
Is it possible change one particular attribute in style sheet without calling setStyleSheet again? Some easy cases I solved by using attribute selectors (similar to CSS) and custom properties that define "type" or "state" of widget.
Let say I need to change border width in run-time.For that I should change attribute "border", and I have defined a custom property borderWidth of type int. CSS does support pseudofunction attr() that could used to compute value of one attribute from another. Apparently it doesn't work with Qt style sheet, e.g.
QTablo [state="3"] {
border: attr(qproperty-borderWidth);
}
after unpolish();polish();update(); routine I see no no changes. Is there a workaround for this other than to set style?
Note that Qt only supports a subset of CSS2, attr() seems to be first mentioned in CSS2.1.
You could:
Use a QFrame and change its properties, instead of using a style sheet.
Generate the stylesheet at run time and assign it. Something like QString("border-width: %1px;").arg(borderWidth);.
Use a custom QProxyStyle that returns the correct value in pixelMetric().
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.
Some posts (namely, these two: Set CSS in QPushButton's subclass's constructor and Does the use of styleSheets in a dynamic manner add a lot of computation) suggest that I should use a single qss file for my Qt application.
However, imagine I have a QLabel subclass whose purpose is (using stylesheets) to provide a particular look/behavior (say BlinkingLabel, however horrendous it would look). In that case, doesn't it make sense to prepare a separate stylesheet and load it in the class constructor? Wouldn't using a global file take away a very useful aspect of using OOP for UI components? Because if I do so, I can no longer just take the class and put it in another project without worrying about anything, because it can take care of itself; I have to remember to copy the relevant stylesheet entries.
Common answer: it depends...
From my point of view, if you will split your qss to several files and implement a good logic to load them correctly - it will be better solution in compare with single file. As extra option, you still may have a global qss with basic styles.
First reason, why splitting is better: your qss will be more readable and it will me more easy to provide a support.
Second: smaller qss files, applied on different widgets works a bit faster.
P.S. If you need to reload qss on a widget (as described under your links), you need to call polish() + unpolish() instead of resetting qss text.
P.P.S. don't use stylsheets for animations. There is a QPropertyAnimation class. You can use it to animate color, font, etc. on your widget.
Is it possible to look up stylesheet values at runtime in Qt?
I'm working on a custom button derived from QPushButton that has some stylesheet properties set. I'd like to be able to look up some stylesheet settings like border width, margin, padding-top, padding-left, padding-right, etc. Is this at all possible to do without calling widget->getStyleSheet() and parsing out the values myself?
Internally, when you call QApplication::setStyleSheet() Qt creates a QStyle sub-class called QStyleSheetStyle.
That means you can query style sheet information via the normal QStyle methods. Just remember to fill in the options and widget parameters properly to ensure you get the right values from the style sheet.
Don't think so, you might be able to find something by stepping through the drawing code. But the parsing and the application of stylesheets is pretty optimised and uses a lot of preprocessing. I don't even think that you can get to the stylesheet of a widget if it was actually set in a parent.
I have a QListWidget containing items which have icons and when the items are selected the icon is just highlighted out. Is there a way to prevent this? I can't use stylesheets because it's for an embedded application and including them takes up too much space.
thanks
I suppose when you say "Highlithed out", you mean that the icon colors don't render well when the line is selected, and therefore, you can't see properly the icon...
Maybe you could consider using a different icon when the item is selected. It's possible to do so by specifing a mode to your icon.
Example :
QIcon MyIcon(":/images/foo");
MyIcon.addFile(":/images/bar", QSize(...), QIcon::Selected);
You can easily make a try in QtDesigner and see the results...
Hope it helps a bit !
Certainly, drawing on a black-and-white screen presents its challenges.
It sounds like you just want to change the appearance of the interface, not any functionality. If this is the case, a QItemDelegate-derived class (or QStyledItemDelegate) is almost certainly what you want. In particular, the drawDecoration function looks like it is used to draw an icon, and the style options should include whether it is selected. The simplest fix would be to override that function, set the selected flag in the options to false, then pass it up to the parent's function.