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. [SOLVED] Custom Widget with custom stylesheet
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Custom Widget with custom stylesheet

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 7.6k 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.
  • B Offline
    B Offline
    butterface
    wrote on last edited by
    #1

    Hi,

    I used the following example to customise my widget through a style sheet: http://qt-project.org/wiki/Qt_Style_Sheets_and_Custom_Painting_Example

    What is the best way to access the correct colour for the current state. For example I have

    @
    MyWidget {
    qproperty-objectColor: yellow;
    }

    MyWidget::disabled {
    qproperty-objectColor: green;
    }
    @

    and my paint method should get the colour depending on the state.

    Thanks,
    Markus

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If i'm not wrong, the property will be updated when your widget stat changes so you simply have to use the objectColor property to retrieve the color in your paintEvent.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on last edited by
        #3

        Unfortunatley not. It stays the same. Here I also have the color property which is from QWidget directly and this one changes (of course?).
        Is it possible that I have to use QStyle somehow? Just using the property does not do the trick. If so the question is how. ;)

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          what type is "MyWidget"?
          I also think the declaration should only contain 1 colon:
          @
          MyWidget:disabled {
          qproperty-objectColor: green;
          }
          @

          Just a hint for the future:

          In your case Qt ensures the polishing, but if you use custom properties in your CSS selectors you have to do like this:

          C++:
          @
          myWidget->setProperty("myProperty", true);
          myWidget->style()->polish(myWidget);
          @

          CSS:
          @
          MyWidget[myProperty="true"] {
          ....
          }
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • B Offline
            B Offline
            butterface
            wrote on last edited by
            #5

            MyWidget is derived from QWidget.

            @
            MyWidget:disabled {
            qproperty-objectColor: green;
            }
            @
            does also not work.

            @
            MyWidget {
            qproperty-objectColor: yellow;
            }

            MyWidget[enabled="false"] {
            qproperty-objectColor: green;
            }
            @

            Is doing now exactly what I want it to do with style()->polish(this) in my paintEvent of the widget.

            Thanks!

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              yea stylesheets are a mystery some/most times. ;)

              But don't call style()->polish(this) in the paintEvent since it's not necessary to do the hard work in every paintEvent call. Only if the property changes. Maybe you can even remove it completely and it will still work?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • B Offline
                B Offline
                butterface
                wrote on last edited by
                #7

                Unfortunately it does not work without the polish. The only thing I can do is to check if anything has changed and call polished then if so.

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  better do the polishing on QEvent::EnabledChange.

                  Edit:
                  another thing which just came to my mind to check: Did you provide the Q_OBJECT macro in your custom widgets class declaration?!

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    butterface
                    wrote on last edited by
                    #9

                    Yes, the Q_OBJECT macro is there. I also use a bunch of signals and slots so it has to.

                    How do I get this event?

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      in QWidget::event()

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        butterface
                        wrote on last edited by
                        #11

                        Works perfect now.
                        Thanks!

                        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