Get properties of qStylesheet - c++

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.

Related

How to get identation width of QTreeView from QStyle

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)

Changing widget's style attribute via qproperties

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

Obtain inherited stylesheet for a QWidget from parents above it

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.

Determining Qt stylesheet options programmatically?

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.

Qt: How to show icon when item selected

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.