Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. G++ would not allow this, take a look before it drives you nuts (example with QPalette)
QtWS25 Last Chance

G++ would not allow this, take a look before it drives you nuts (example with QPalette)

Scheduled Pinned Locked Moved C++ Gurus
26 Posts 8 Posters 10.3k Views
  • 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.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by
    #1

    Oddly enough QWidget::palette returns a const &QPalette, but:

    g++ does not allow to call through a constant reference a member function which modifies its instance.

    Which makes sense.

    It will scream upon:
    @some_label->palette().setColor(QPalette::WindowText, QColor(Qt::blue));
    @
    While accepting:
    @(const_cast<QPalette &>(some_label->palette())).setColor(QPalette::WindowText, QColor(Qt::blue));

    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      I don't know why QWidget::palette returns a const reference and not a value (probably it's a fine-tuned optimization) -- QPalette is a value based class.

      Either way, you're not supposed to do that. Do this instead:

      @
      QPalette palette = widget->palette();
      palette.setColor();
      widget->setPalette(palette);
      @

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        You will admit it' s a bit contrived.

        I may not be supposed to do this, as you say, however g++ accepted it without
        the slightest warning. Which is quite reassuring.

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

          The compiler accepted it, but did it actually work? Was your palette updated, and the rendering of your widget with it? That would suprise me, really. The setPalette method will obviously trigger the update for the widget, because the palette() method is const and is not supposed to change the state anyway.

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

            Clearly you read my post a bit too quickly.

            QWidget::palette() is const, yes, but does not modify anything in this process.
            It returns a constant reference to a QPalette whose setcolor() is then called and
            modifies the instance, that' s its job. The const_cast makes this call possible.

            Please note it does work (platform: Linux Fedora 15 x64).

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

              No, I think you read my reply a bit too quickly. That the palette is modified I will believe, but what I am wondering is if the widget is actually redrawn when you do this.

              1 Reply Last reply
              0
              • ? This user is from outside of this forum
                ? This user is from outside of this forum
                Guest
                wrote on last edited by
                #7

                The QLabel' s palette is modified before the label becomes visible.
                The label is packed in a QVBoxLayout and shows up with the right
                colour when the parent window of the layout is made visible.
                That' s all. Now if you are still skeptical, try it.

                Note: when I said it worked I obviously meant the label showed up with
                the expected colour, not that the palette was presumably modified.

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

                  [quote author="Quicksort" date="1317656223"]
                  While accepting:
                  @
                  (const_cast<QPalette &>(some_label->palette())).setColor(QPalette::WindowText, QColor(Qt::blue));

                  @
                  [/quote]

                  You're telling g++ to shut up. And it obeys.

                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

                  1 Reply Last reply
                  0
                  • ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on last edited by
                    #9

                    Well, that' s perhaps why casts are so helpful !
                    Be that as it may, I don' t see the point in the three
                    steps maneuver where a simple cast does the trick.
                    Why would it be safer to create a new palette and
                    then use setPalette() than to talk the member
                    function into doing what it' s meant for ?

                    Regards.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #10

                      [quote author="Quicksort" date="1317675418"]Well, that' s perhaps why casts are so helpful !
                      Be that as it may, I don' t see the point in the three
                      steps maneuver where a simple cast does the trick.
                      Why would it be safer to create a new palette and
                      then use setPalette() than to talk the member
                      function into doing what it' s meant for ?

                      Regards.[/quote]

                      You're assuming that there is a QPalette object in the widget itself which the palette() method returns and that you should just be able to operate on that object, i.e., you want an accessor to an internal member. Apparently, in this case, there does happen to be a QPalette object in the widget that can be modified (using a const cast to "break the rules.")

                      However, imagine the scenario where, while you assume that there's just an object sitting in there, there might not be. Perhaps the palette() method might pull arbitrary bits and pieces of information from within the object, and store them in some temporary internal working QPalette and return a reference to that. That's a contrived scenario, granted, but it's the root of what data encapsulation is all about. There is a palette() getter and a setPalette() setter because it decouples the inner workings of how the data is stored from the outside user interface.

                      By casting palette() as non-const and then modifying the values, you're making a big assumption about how the class works internally. The fact that it does is complete happenstance and is not guaranteed to work now or at any time later, until such time that the interface changes. It's safer to use the three-step method simply because that's the way the class is documented to work. That's the specified interface in the API.

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                        You need a const_cast to do what you want while you are using the public API of a third party library. I would clearly call this a serious design failure - at best.

                        And as Franzk already mentioned, const_cast is a kind of shut_up_I_know_what_I_do_cast, so I do not see any reason of what to discuss on that topic, and certainly not with such an "alarming" topic. You did not discover anything new nor anything that drives someone nuts.

                        Keep calm and hack on.

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

                        1 Reply Last reply
                        0
                        • ? This user is from outside of this forum
                          ? This user is from outside of this forum
                          Guest
                          wrote on last edited by
                          #12

                          Very well, but it would be way simpler to have a few QWidget
                          access functions allowing to safely modify some QPalette settings.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dangelog
                            wrote on last edited by
                            #13

                            And what's wrong with what I said before?

                            [quote author="peppe" date="1317657653"]
                            @
                            QPalette palette = widget->palette();
                            palette.setColor();
                            widget->setPalette(palette);
                            @
                            [/quote]

                            Software Engineer
                            KDAB (UK) Ltd., a KDAB Group company

                            1 Reply Last reply
                            0
                            • ? This user is from outside of this forum
                              ? This user is from outside of this forum
                              Guest
                              wrote on last edited by
                              #14

                              With a single call, without all this fuss, Mr Specialist.
                              Got it ?

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                Franzk
                                wrote on last edited by
                                #15

                                There's no need to use that tone.

                                It wouldn't be a single call:
                                @widget->palette().setSomething();@
                                Is already two calls. For one setting you might consider the 'proper' approach boilerplate, but there are more considerations going on here.

                                setPalette() is more explicit about the change to the widget palette than palette().setBrush()

                                widget->palette().setBrush() would be exactly like having a public variable. The fact that const & is returned is probably just for speed purposes (arguably it shouldn't even be there).

                                widget->setPalette() allows widget to properly and immediately react to palette changes, by calling update() for example.

                                returning non-const references encourages code like

                                @widget->palette() = mypalette;@
                                which is generally considered bad coding (unintuitive).

                                "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

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

                                  Well, I tried your code, and it is exactly as I predicted: it does not work. That is: it works if you change the palette before a widget is shown. If you change it after it has already been shown, the label is not redrawn. That is what I was asking:

                                  [quote author="Andre" date="1317662722"]No, I think you read my reply a bit too quickly. That the palette is modified I will believe, but what I am wondering is if the widget is actually redrawn when you do this.[/quote]

                                  Others have already explained why it is not a good idea. Personally, I find hard-to-read const casts more fuss than three clearly readable lines, but that is just me.

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    dangelog
                                    wrote on last edited by
                                    #17

                                    [quote author="Quicksort" date="1317685193"]With a single call, without all this fuss, Mr Specialist.
                                    Got it ?[/quote]

                                    It's nonsense to use that attitude. Write an inline function so you can modify your palette with a single line of code.

                                    Software Engineer
                                    KDAB (UK) Ltd., a KDAB Group company

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

                                      [quote author="Quicksort" date="1317685193"]With a single call, without all this fuss, Mr Specialist.
                                      Got it ?[/quote]

                                      Please calm down your wording. People are trying to explain things to you. There is absolutely no reason to become personally insulting.

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

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        tobias.hunger
                                        wrote on last edited by
                                        #19

                                        Why are you claiming this is a g++ issue?

                                        I would be surprised if other compilers did not issue a similar error: Modifying a const reference is wrong.

                                        Your "fix" by forcing it to be non-const is rather questionable though: It might or might not work, depending on how the widget is implemented, ruining the encapsulation OO design is all about.

                                        1 Reply Last reply
                                        0
                                        • ? This user is from outside of this forum
                                          ? This user is from outside of this forum
                                          Guest
                                          wrote on last edited by
                                          #20

                                          ->Franzk:

                                          As for my inappropriate tone, let me tell you I am a bit tired of the patronizing
                                          one of the forum' s divas. Now, I just suggested to add a few QWidget
                                          functions allowing in one programming step to modify some palette settings.
                                          It would translate in several function calls behind the scenes but would ease
                                          programmer' s task.

                                          ->Andre:

                                          I have clearly stated that the label is not yet visible when this palette setting
                                          is modified. Since you have established it does not work if the widget is
                                          already visible, your method is clearly the right one although it remains
                                          quite contrived due to the lack of QWidget functions doing the job in one
                                          programming step.

                                          ->Others:

                                          Nothing.

                                          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