How to save style before changing it?
Page |
1 |
I want to make some changes to the application’s global style, but want to save the default style, for use in certain widgets.
I don’t see anything in QStyle or QCommonStyle that would help me achieve it.
How to do it?
20 replies
Actually, that is in interesting question. A quick documentation scan does not reveal an easy way to do this.
The main problem is, that the suggestion to just take the pointer and keep it around, and set it for the widgets you mention, will not work. QApplication will delete the curent QStyle object as soon as you set another one. That makes that you cannot really use the pointer anymore after that.
On the other hand, I do not see a way to query the name of the current QStyle. I might have overlooked it, but I would have expected QStyle to provide a name for the style. If you can find it, you can instantiate a new style object based on the name.
The main problem is, that the suggestion to just take the pointer and keep it around, and set it for the widgets you mention, will not work. QApplication will delete the curent QStyle object as soon as you set another one. That makes that you cannot really use the pointer anymore after that.
Confirm, in fact within QApplication::setStyle we have
- if (old && old->parent() == qApp) {
- delete old;
- }
being old the previous QStyle*. Being setStyle a static method there is no way with subclassing of change the behavior.
I guess the only solution is to create a data structure to store properties about the old style. Or to make a subclass that reveals the d-pointer, with of course a lot of portability issues…..
You can set a style on a widget too. That is not the issue:
Setting a style on a widget will override the application style.
However, I guess you should heed the warning from the documentation:
Warning: This function is particularly useful for demonstration purposes, where you want to show Qt’s styling capabilities. Real applications should avoid it and use one consistent GUI style instead.
You must log in to post a reply. Not a member yet? Register here!




