Using a stylesheet breaks palette of children objects.
-
Hello there.
I'm having a problem with stylesheets. I would like to apply a style to all QScrollBars in my application but I don't want it to mess with the other widgets.
The problem is, some widgets like the calendar will simply ignore their palettes when the application has any stylesheet, even if it affects only the QScrollBar.
Here are some steps quickly reproducible on QtDesigner:
1-Create empty widget X (so it creates a window displaying that widget).
2-Add a calendar Y to widget X (drag-drop on window).
3-Set a palette for calendar Y (e.g.: black so you can see easily)
4-Add the following stylesheet to the widget X: "QScrollBar {foo: bar}"
Result: The calendar ignores the palette.I've tested on both Qt4 (4.8,4.8.6) and Qt5 (5.2,5.3,5.3.1,5.3.2) with the same output.
The problem is, I need to use stylesheets on the scrollbar because I want to modify the sizes of the handlers and buttons, and I also need to set palletes of calendars because afaik there's no way to customize the active/inactive/disabled with stylesheets nor set WeekdayTextFormats.
Perhaps there's a way to get the QScrollBars of a QScrollArea so I can subclass it and apply the stylesheet directly to every individual scrollbar in my app.
I don't know what's the best way to solve the problem.
Any help is appreciated. Thank you in advance.
-
Hi and welcome to devnet,
You can write your style sheet such as only the scrollbars from certain widget are styled e.g.
@
QTableWidget QScrollBar { background : red;}
@will make all QScrollBar that are child of QTableWidget background red. Doing so should leave your calendar alone
Hope it helps
-
Hi there. Thank you. However, that is pretty much the same thing as the "QScrollBar {foo: bar}" mentioned above. It's just a rule that should select only the QScollBar and leave the other stuff alone; that's not what happens though. Please try reproducing these simple steps to have a picture of the situation (can be done easily in QtDesigner):
1-Create a Widget
2-Add a calendar as a child of such widget
3-Apply some palette to the calendar; so far it works as expected
4-Set the stylesheet "QScrollBar {foo: bar}" to the parent widget, now the calendar won't respect its individual palette any longer.You don't even need a 'parent widget' in fact. I was trying to apply a stylesheet directly to my application, but that caused the palettes of various widgets to stop working, even if the stylesheet is just "QScrollBar {foo: bar}". Also, the stylesheet doesn't have to be significant, even just "#" or "/**/" will 'break' the palettes of other widgets.
In the mean time I've applied a stylesheet to each scrollbar in my application (not manually obviously), this way the other widgets aren't affected. I don't think that's the way it's supposed to work though.
I should also take a look at the qml and other ways to "style" my applications, palettes and stylesheets are the same since early qt4s and I don't think they're going to be worked on... If only the stylesheets were more "CSS3" compliant and allowed for a deeper customization... I don't know what is the best way to style an application; it's sure a pain to keep overriding paint events and doing stuff manually :(.
EDIT:
Here is an SSCCE in Python (Qt5, PyQt5, it's a pity PySide is dead :( ):
http://goo.gl/UIYvkNEDIT2:
I could provide a C++ example but gcc went nuts in this machine so I can't test it, and python is readable enough imho, I like it better than C++ for examples :). It should be easy enough to translate that into C++. -
Hey guys, I see this thread was buried... I solved my specific problem, but I'd like to know why that happens and if there's a way to prevent that behaviour.
Also, I'm from the Trolltech days (those were the days! :) ) and it seems using stylesheets/palettes isn't the best way to make a deeply 'styled' GUI nowadays, mostly because stylesheets are very limited and won't play well with palettes. I'd like to know however what is the best way to achieve a very customized UI without having to deal with drawing directly and using lots of images... it seems QML is the new thing for styling a GUI, but I'm not sure if it's styleable enough.
-
It's not buried at all, some patience might be required since it's a community driven forum.
What limits are you hitting with your style sheet ?
By the way you can access the palette from within the style sheet.
Depending on your mods then maybe QStyle might be of interest.
-
Hi,
[quote author="Beliar" date="1422570231"]The problem is, some widgets like the calendar will simply ignore their palettes when the application has any stylesheet, even if it affects only the QScrollBar.[/quote]Yes, style sheets and palettes conflict with each other, unfortunately. That is a documented limitation: http://doc.qt.io/qt-5/qwidget.html#palette-prop
'Warning: Do not use this function in conjunction with Qt Style Sheets. When using style sheets, the palette of a widget can be customized using the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color".' ("this function" refers to setPalette())
[quote author="Beliar" date="1422627239"]I'd like to know however what is the best way to achieve a very customized UI without having to deal with drawing directly and using lots of images...[/quote]I believe you have 3 options:
Use stylesheets only, and not palettes
Use palettes only, and not stylesheets
Use QML (Qt Quick)
[quote author="Beliar" date="1422570231"]it seems QML is the new thing for styling a GUI, but I'm not sure if it's styleable enough.[/quote]Styling capabilities were added in Qt 5.1: http://doc.qt.io/qt-5/qtquickcontrolsstyles-index.html
As for "is it styleable enough?", that depends on how much styling you want, I guess. Have a play with it and find out.
-
Thank you guys!
@SGaist: Sorry, I saw this thread was not on the first page and thought it were dead :D.
@JKSH: I totally missed/forgot that... in fact, the last time I read the docs carefully were just after the qt4 release. Since then I almost never read it, except for the new stuff.
Please, don't get me wrong guys, I really love stylesheets, but it could always improve. It's a little bit tricky to select stuff correctly (e.g.: using (!)active/(!)enabled and hierarchy selectors (>>)), but I've been using them to some extent for many years so I'm somewhat used to it, however, as I said, it would be good if it were more "CSS3 compliant", and if there were more styleable stuff. You can accomplish a lot using for example border-image with external images and 'selecting' the objects carefully, since you can style most 'child' objects of the built-in widgets, but you still have to deal with direct drawing (e.g.: overriding the paintEvent and using drawPath, etc.) to customize more deeply something. I think Qt could provide ways to use css selectors to style individual components of the more complex widgets more easily.
It's no big deal though, and surely there are other priorities that are much more important. My question was mostly on why stylesheets don't play with palettes, and that's just a limitation (according to the docs). Simple as that.
Thank you guys. For now I'm going to keep using stylesheets (and I'm not going to use palettes any longer) for the stuff that needs 'serious' styling, and perhaps in the future I'm going to take a loot at QML.
EDIT:
@JKSH: I don't think the option #2 is good because palettes are more focused on 'colouring' stuff. No idea on #3. -
What kind of modification do you do that require custom paintEvent ?