Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to save style before changing it?
Forum Updated to NodeBB v4.3 + New Features

How to save style before changing it?

Scheduled Pinned Locked Moved General and Desktop
21 Posts 7 Posters 8.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            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:

            @QStyle* myOldStyle = style();
            // change your style here

            @

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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              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
              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fluca1978
                  wrote on last edited by
                  #8

                  [quote author="Andre" date="1323080251"]
                  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.
                  [/quote]

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

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

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

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fluca1978
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        [quote author="fluca1978" date="1323090473"]Of course style is shared among the whole application. The problem here was also about how to swtich back to an already used style.[/quote]

                        That was one part. The other question was

                        [quote author="Asperamanca" date="1323078786"]What I want is

                        • to be able to explicitly set the old style on certain widgets[/quote]

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          You can set a style on a widget too. That is not the issue:
                          @
                          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:
                          [quote]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.[/quote]

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Asperamanca
                            wrote on last edited by
                            #13

                            Well, I could do an ugly hack like this:

                            @
                            if (qobject_cast<QWindowsVistaStyle*>(QApplication::style()))
                            {
                            sm_pLocalStyle = new QWindowsVistaStyle();
                            }
                            else if (qobject_cast<QWindowsXPStyle*>(QApplication::style()))
                            {
                            sm_pLocalStyle = new QWindowsXPStyle();
                            }
                            else
                            {
                            sm_pLocalStyle = new QWindowsStyle();
                            }
                            @

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              broadpeak
                              wrote on last edited by
                              #14

                              [quote author="Andre" date="1323093315"]
                              Setting a style on a widget will override the application style.
                              [/quote]

                              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().

                              1 Reply Last reply
                              0
                              • L Offline
                                L Offline
                                lgeyer
                                wrote on last edited by
                                #15

                                Well, QStyle is an QObject, so retrieving the name of the current style is as simple as ...
                                @
                                QString currentStyle = qApp->style()->metaObject()->className(); // eg. 'QWindowsVistaStyle'
                                @

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  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.

                                  1 Reply Last reply
                                  0
                                  • V Offline
                                    V Offline
                                    veeeee_d
                                    wrote on last edited by
                                    #17

                                    You can deep copy the style and just restore it later, can't you?

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      lgeyer
                                      wrote on last edited by
                                      #18

                                      [quote author="veeeee_d" date="1323105034"]You can deep copy the style and just restore it later, can't you?[/quote]

                                      You can't clone a QObject.

                                      [quote author="Andre" date="1323104776"]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. [/quote]

                                      What's the difference between the style name and the class name? What's the advantage of one over the other?

                                      As far as I've seen QStyle unfortunately does not provide any invokable constructors so you'll need a simple factory which creates a QStyle out of a given (class) name.

                                      (I wonder why QObject subclasses do not have at least one invokable (default) constructor.)

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        andre
                                        wrote on last edited by
                                        #19

                                        Indeed, that would solve the issue. I also wonder why the QStyle classes don't have a method like styleName() that returns the name of the style that you can pass to QApplication::setStyle(QString name). The difference between the class and the style name is at least the "Q", but perhaps more.

                                        By the way: I noticed the same types of missing features for the Qt SQL drivers: all you can get is a list of plugin names for the drivers, but nothing that you might want to present to the end user.

                                        One final issue that comes to mind is this: are you (topic starter) certain that it actualy works to change the style during the application run? Because I think that that is not really supported. The docs for QApplication::setStyle() even suggest you set it before you actually construct the QApplication object. That makes me wonder how dynamic style changes are going to be. I never tried it myself, but it would not supprise me that dynamic style changes are not supported at all, and you run into all kinds of layout and rendering issues if you try.

                                        1 Reply Last reply
                                        0
                                        • V Offline
                                          V Offline
                                          veeeee_d
                                          wrote on last edited by
                                          #20

                                          Or maybe they could be exported as a Style Sheet, but I believe that would be a little complicated to develop.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved