December 5, 2011

Asperamanca Asperamanca
Hobby Entomologist
371 posts

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

December 5, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

Not sure, but saving a pointer to the style before applying it does not suffice?

December 5, 2011

Asperamanca Asperamanca
Hobby Entomologist
371 posts

How will saving a pointer help me when I change the style? A pointer is just a pointer. I need a copy or clone functionality, or a way to do what QApplication does when starting up, that is, constructing the correct style for the current environment.

December 5, 2011

Andre Andre
Area 51 Engineer
6031 posts

Perhaps you should try to rephrase what your real goal is. It is not very clear what you want to achieve in the end.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 5, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

If what you want is to be able to switch back to your original style, maybe thru a menu item, something like this should suffice:

  1. QStyle* myOldStyle = style();
  2. // change your style here

but as Andre stated, you should express better what is your aim, so we can help.

December 5, 2011

Asperamanca Asperamanca
Hobby Entomologist
371 posts

What I want is

  • to be able to switch back to the old style
  • to be able to explicitly set the old style on certain widgets

December 5, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 5, 2011

fluca1978 fluca1978
Ant Farmer
524 posts
Andre wrote:
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

  1.  if (old && old->parent() == qApp) {                                                    
  2.         delete old;                                                                        
  3.     }  

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…..

December 5, 2011

Volker Volker
Robot Herder
5428 posts

A style is a global object, you cannot set a different style for a selected set of widgets.

December 5, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

Of course style is shared among the whole application. The problem here was also about how to swtich back to an already used style.

December 5, 2011

Volker Volker
Robot Herder
5428 posts
fluca1978 wrote:
Of course style is shared among the whole application. The problem here was also about how to swtich back to an already used style.

That was one part. The other question was

Asperamanca wrote:
What I want is
  • to be able to explicitly set the old style on certain widgets

December 5, 2011

Andre Andre
Area 51 Engineer
6031 posts

You can set a style on a widget too. That is not the issue:

  1. QWidget::setStyle(theOtherStyle);

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 5, 2011

Asperamanca Asperamanca
Hobby Entomologist
371 posts

Well, I could do an ugly hack like this:

  1.     if (qobject_cast<QWindowsVistaStyle*>(QApplication::style()))
  2.     {
  3.         sm_pLocalStyle = new QWindowsVistaStyle();
  4.     }
  5.     else if (qobject_cast<QWindowsXPStyle*>(QApplication::style()))
  6.     {
  7.         sm_pLocalStyle = new QWindowsXPStyle();
  8.     }
  9.     else
  10.     {
  11.         sm_pLocalStyle = new QWindowsStyle();
  12.     }

December 5, 2011

broadpeak broadpeak
Hobby Entomologist
360 posts
Andre wrote:
Setting a style on a widget will override the application style.

The widget style is usually the same for all widgets in an application (QApplication::style()), but it can be overridden on a per-widget basis using QWidget::setStyle().

December 5, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Well, QStyle is an QObject, so retrieving the name of the current style is as simple as …

  1. QString currentStyle = qApp->style()->metaObject()->className();  // eg. 'QWindowsVistaStyle'

December 5, 2011

Andre Andre
Area 51 Engineer
6031 posts

But the class name is not the same as the style name, that you need to instantiate it. Although, I think you might be able to instantiate the object using the class name.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

Page  
1

  ‹‹ About setting up server      Menubar uses colors not defined in QApplication::palette() ››

You must log in to post a reply. Not a member yet? Register here!