I have a multi-platform Qt application with a custom palette that we have already set up to produce the look that we want. The palette was set on the top level widget and so this was inherited by all of the child widgets.
I then wanted to dynamically change the text colour of some QPushButtons. Searching around for the best way to do this, I began reading about Style Sheets which I didn't know that much about previously. These seemed to be a good option for changing the look of the push buttons and they did this. However, as soon as I set the text colour using a style sheet, the widget palette jumped to the default. It soon became obvious that style sheets are not compatible with custom palettes and it wasn't possible to mix the two. Most of the info that I found suggested that style sheets should be used rather than custom palettes because, among other things, style sheets offer guarantees across platforms and looks which palette fiddling does not.
From this, it seemed like a good idea to convert my custom palette into a style sheet and set it on the top level widget. Then I would easily be able to change just some parameters of certain child widgets using sub-style-sheets. The problem is that the palette contains many colours that we have set that do not appear to have a style sheet equivalent. Eg, Light, Midlight and Dark. Without being able to set these colours I wasn't able to produce the same look using style sheets as I previously had with the customised palette.
I would like to know if there is a simple way to convert a customised palette into a style sheet or whether there is some documentation to describe the equivalent style sheet settings for each palette colour. I have done searches on google, stackoverflow and the Qt reference docs but have come up short.
Thanks for any info.
ps. I solved my original problem by dynamically modifying the ButtonText attribute of the palette for the QPushButtons. But I would still rather use style sheets instead if possible without changing the main look of my application.
QColor.name() is the hex string for that color. If you want to convert to a stylesheet, just run through all the roles in your palette and print the .name() of that color. You can then find the corresponding stylesheet properties and pseudo states to set the hex colors to.
One thing you may find frustrating from switching from palettes to stylesheets is that CSS
does not allow the use of variables, so you'll have to use the hex string everywhere, rather than just defining named colors and using those.
Related
I have to update the nodes styling dynamically based on my data. Mostly it will be background, border and font. I cannot define predefined css class names to define background color as the colors will be dynamic. Same as with border and fonts.
I am aware that I can set background to a Region in two ways. By constructing the style string and set in setStyle method
region.setStyle("-fx-background-color:"+getColorStr()+";");
or alternatively I can construct a Background object and set using setBackground.
Background background = new Background(new BackgroundFill(getColor(), new CornerRadii(0), Insets.EMPTY));
region.setBackground(background);
I am a bit confused to choose which option to go with. Mainly worried about the performance issue of one option over other. Can someone let me know which is most preferrable way to go with (considering performance) ?
Just to let you know my CSS file is a heavy file with complete customisation of all controls. Can this be a factor to consider for performance if I go with setStyle method.
The same question when dealing with Border and Font as well.
I want to make a theme where a user can input one hexadecimal color for a theme. but a theme has made by various pre-defined color.
So my question is that how to know offset between two colors code and using that offset generate a new color,
So for example:
If a user selects color code #110360 then theme header color should be #1c3ea4. so now how to know offset between it, so if user change color then calculates a header color using that offset.
I am using sass to design theme
Thank you
You will have to use Javascript for something like this unless you are able to compile your Sass on the fly when a user chooses their base color.
The SO answer here seems to have what you're looking for: https://stackoverflow.com/a/9821101/5463842
I've been searching for a while for an answer to this problem and I'm rather surprised that I have not found a solution. I'm working with Qt on Mac and would like to customize QPushbuttons and other QWidgets, but I want to maintain the native look somewhat as well. For example, if I want to remove the margins of a QButton using style sheets, I do:
QPushButton btn(this);
...
btn.setStyleSheet("margin:0;");
This indeed removes the margins, but it also removes the native style already set for default buttons. Of course, I just want to modify margins using style sheets, how do I do this?
Also, I would expect that btn.styleSheet() would return the native style sheet, but it is blank by default. Only when I set my own style sheet, does it return a valid value, but only for the property I set. I get that setStyleSheet would reset the style sheet, but how do I modify certain properties and leave everything else as is?
TL;DR: It can't be done that way.
The native style cannot be generally expressed as a CSS style sheet, thus styleSheet() is empty by default on all styles. Thus, unfortunately, it's not possible to change native style elements one-by-one, since typically they are drawn by the platform APIs that allow little if any customization.
For examples, a QPushButton is drawn by the native calls on both OS X and Windows.
In the specific case of the margin, though, you can easily work around by creating a proxy style that returns smaller control rectangle and crops and transforms the painter before passing it to the base style. This also works for colorization/color substitution etc. You basically have to accept that the base style has to do the drawing, and then it's up to you to tweak it.
I am using a pretty lightweight custom palette that just defines a few colors and properties. I am attempting to transform it into a stylesheet but I am running into problems. For example, what is the QPalette::Base equivalent in stylesheets? Do I have to change the background color for every widget individually?
I tried setting a few stylesheet properties for the QWidget but then I got undesired results for widgets with borders. Layouts were showing borders when I didn't want them to, so I found myself writing a lot of stylesheet definitions to add/remove borders.
The gist of the question is, is there a good way to convert a palette to a stylesheet?
I have a pyqt4 app which should run with arbitrary colorscheme. I would like to make some widgets more visible by making them lighter or darker than their style-predefined color. I would like it to work for any overall style and don't want to hardcode colors in there. What would be the way to call QWidget.setStyleSheet which would make that happen?
I'm not sure this is possible. There is no default stylesheet to query and parse, because its a user-specified value.
Themes can be platform-dependent, so would have to do a bunch of processing on the widgets style -> palette -> attributes, figure out the color values, and generate a modified value to specify in your stylesheet. At that point, you are better off just sticking with the palettes and modifying their values, and not worrying about a stylesheet.