PDA

View Full Version : Style Sheets: re-using/referencing builtin pixmaps



chezifresh
23rd March 2009, 21:44
I'm trying to create a style sheet that uses one of the builtin icons (http://doc.trolltech.com/4.4/stylesheet-reference.html#list-of-icons)

I really want to reference the 'downarrow-icon' but I'm not sure if there is a syntax available to support it.

Off the bat I tried using Qt's documentation for help and tried this:

QFooBar {
image: url(downarrow-icon);
}

It works fine when you give it a path in the resource system just not a builtin icon.

ex. works:
QFooBar {
image: url(:myIcon.png);
}

Any ideas?

wysota
24th March 2009, 08:39
It's not like that. It works the other way. The mechanism is made available so that you can substitute one of the predefined images (like "downarrow-icon") with your own. So the syntax would be:
something {
downarrow-icon: url(:/myicon.png);
}

chezifresh
24th March 2009, 17:35
Yeah I was afraid of that. Its certainly a shame you can't reference builtin icons. It could be a powerful tool for custom widgets. I'm a fan of keeping a consistent look and feel and using builtin icons and system icons are something that Qt seems to be missing.

Though with Qt 4.5 it appears we can now use some system icons (I've yet to play with it since I'm still waiting on PyQt 4.5 to be released)

wysota
24th March 2009, 19:30
Yeah I was afraid of that. Its certainly a shame you can't reference builtin icons.
Sure you can. It's a bit more complicated but certainly possible. Qt allows you to create artificial file systems (like the resource file system) where you can add whatever data you want. Standard pixmaps included.

And if you don't have to use stylesheets, you can reference the icons directly using QStyle API.

chezifresh
24th March 2009, 22:01
Absolutely, I was just hoping I could pull it off in a stylesheet. My specific use case for this is that I'm building a QTreeView that has combo boxes as persistent widgets. I want the combo boxes to be clear with no borders (basically just showing the text and the down arrow).

I bit this off by using the stylesheet to make the borders and backgrounds transparent. The down side is that there seems to be a bug in the QComboBox::drop-down if you set the stylesheet on it you lose the indicator. So I'm re-applying the indicator with a neutral color pixmap (to work with dark/light color schemes). So this conversation really came about because of quirk in the stylesheet. I can probably accomplish this much better by subclassing the combo box, I was just looking for something quick and dirty for the time being and ran into this.

Thanks for your help though, its good to know this isn't supported through stylesheets

Kal
23rd June 2009, 19:18
I found this on one of the three main Stylesheets reference pages:

Note: With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.

This might explain why your arrow disappeared when you customized part of the combobox but you may have already known the nature of the quirk.

Fastest way I can think of would be to programmatically save out the icons you want via the non-static QStyle::standardIcon(QStyle::SP_ArrowDown), etc. With those image files on your drive, you can compile an external Resource that your stylesheets can access at runtime (assuming the resource syntax holds for stylesheets).

You may not be able to drop those "SP_" enums right into the stylesheet code but "url(:/qtStandard/downArrow)" seems reasonable.