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.
Related
I want to be able to create an simple internal theming system for my program (by internal I mean I don't need to be able to load custom themes from user system... all themes will be embedded inside the program itself.)
I can think of some ways:
simply put all color inside the main stylesheet of the program, every time the colors need to change, retrieve main stylesheet, parse and change the colors, and then reapply it again (or have multiple stylesheet with different colors, and apply them when its needed).
Create a top widget that only hold the colors for each widget, and then a child widget that hold other stylesheet for other styling, and all the UI as child of this widget... in this way we need to only change the first stylesheet.
... (not sure about other ways, but I'm sure there is some... maybe using QStyle or something..?)
I'm not sure what is the best way to achieve what I'm after, that will be best at both performance and the code itself...
I know the first way will be heavy in performance side, to each time change the whole stylesheet and rerender the whole program. (I think at least)
the second option though, I'm not sure how it will work, I mean its any better then the first one?!
or there is any other better methods..?
you should create a .qss file and add it in your .qrc like you add your icons.
then write all your stylesheet there. you can have multiple Style.qss files and these files are your themes and whenever you change it your theme will change.
add your Style.qss File in your Resources(.qrc)
write these codes in main.cpp
Load the application style
QFile styleFile(":/style.qss");
styleFile.open(QFile::ReadOnly);
Apply the loaded stylesheet
QString style(styleFile.readAll());
a.setStyleSheet(style);
For more information :
https://github.com/GTRONICK/QSS
https://github.com/ColinDuquesnoy/QDarkStyleSheet
I have pre-existing code which has a tree view like appearance but doesn't actually use QTreeView (uses QListWidget with custom paint).
I am tasked to add the indentation between the parent and child objects, but I can't able to find how to get the style default indentation width from QStyle. The program has to run on a variety of os and allow to use custom stylesheets. So it's important to get this option from QStyle.
Regards.
found this in qt sources
have to use QStyle::pixelMetric with QStyle::PM_TreeViewIndentation
inside your widget's procedure
ident = style()->pixelMetric(QStyle::PM_TreeViewIndentation)
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.
For my aplication I use qStylesheet. In a custom widget I draw some things overriding the paintevent method. I want to use the colors of this widget, set by the qStylesheet to achieve consistency, and later on configurability. How can I get the properties set by the qStylesheet?
Try to get QPalette and get current properties. For example:
widget.palette().brush().color();
Another way is to get stylesheet and parse it (maybe with regular expressions), but I think it is not the best approach.
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.