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. Custom sub-controls for custom widgets in style sheets
QtWS25 Last Chance

Custom sub-controls for custom widgets in style sheets

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 9.4k 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.
  • M Offline
    M Offline
    MikeHammond
    wrote on last edited by
    #3

    Apologies for not being clear: I want to apply a style to subcontrols within a single Qt widget. That is, the custom MyNewWidget doesn't have any children, just components.

    As an example, imagine I created a QSwissCheese widget, which allows the user to draw the holes on the cheese background. I might wish to style it like this:

    @QSwissCheese
    {
    background-color: rgb(255, 240, 0);
    color: rgb(0, 0, 0);
    }
    QSwissCheese::holes
    {
    background-color: rgb(0, 0, 0);
    color: rgb(255, 255, 255);
    }
    @

    The widget background would be as simple as calling QPainter::fillRect() with my background palette colour. I would then like to draw the holes (as circles) using the holes palette.

    Any thoughts?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vinb
      wrote on last edited by
      #4

      If i understand you correctly,
      you have holes wich are only 'visible' on the cheese and you want to control its appearance with a stylesheet?

      Look at "this":http://doc.qt.nokia.com/4.7/stylesheet-syntax.html#sub-controls and "this":http://doc.qt.nokia.com/4.7/stylesheet-examples.html

      I dont know if you can get something round using stylesheet, but you can try 'border-radius : 10px' or something like that and see if it fits you. but thats not absolute round.

      If that doesnt helps you, mayby you can paint the holes to.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MikeHammond
        wrote on last edited by
        #5

        Yes, in this example, I have a single widget which is a SwissCheese widget. I am painting both the background and the holes (using QPainter::drawEllipse() or QPainter::fillPath() or something similar). I want to be able to specify two different palettes, one for the main widget and one for the holes, in a stylesheet, as in the above example.

        Unfortunately, because my widget does not inherit from a widget with subcontrols, using the standard subcontrols (as you suggested "here":http://doc.qt.nokia.com/4.7/stylesheet-syntax.html#sub-controls), it is not sufficient to use an existing subcontrol; instead, I want to define a new subcontrol ("holes"), and get its palette in the paint event.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vinb
          wrote on last edited by
          #6

          Why not make an object of the 'hole', then it is for sure possible to alter it with the stylesheet.
          With your case, i dont know if its possible or how to do it.

          Edit:
          look at "this":http://doc.qt.nokia.com/4.7/stylesheet-examples.html#customizing-qdockwidget section.
          where i can see its is possible to use stylesheets on signal events to. So mayby that can help you?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MikeHammond
            wrote on last edited by
            #7

            (I would still like to get an answer as to whether I can have a custom subcontrol in a style sheet. But putting that aside for a second...)

            Just to be clear, is this what you're proposing?

            Create a class called QSwissCheeseHole

            Create a QSwissCheese object with one (or more) QSwissCheeseHole objects

            When the QSwissCheese::paintEvent() is called, ask the QSwissCheeseHole class to render itself given a particular render rectangle

            In the style sheet, put:

            @QSwissCheese
            {
            background-color: rgb(255, 240, 0);
            color: rgb(0, 0, 0);
            }
            QSwissCheese > QSwissCheeseHole
            {
            background-color: rgb(0, 0, 0);
            color: rgb(255, 255, 255);
            }
            @

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vinb
              wrote on last edited by
              #8

              Something like that yes, look at my edit in my last post also please.

              1. @ QSwissCheese
                {
                background-color: rgb(255, 240, 0);
                color: rgb(0, 0, 0);
                }
                QSwissCheeseHole
                {
                background-color: rgb(0, 0, 0);
                color: rgb(255, 255, 255);
                } @
                this would be enough then.

              -And yes, you can have custom subcontrol in your stylesheet.-
              deleted my last sentence after the input from Andre. :)

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

                Without doing your own parsing of the style sheets involved, you can not do this. At least not using public Qt API. The stylesheet-based rendering is burried quite deep in the Qt widget rendering stack.

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  vinb
                  wrote on last edited by
                  #10

                  I retread quietly now,
                  Andre is a better help then me in (not only) this case. :)

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

                    This sentence from the Qt Style Sheet documentation contains a hint towards the problems associated:
                    [quote]Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release.[/quote]

                    I have the impression, that even this plan is not on the roadmap. QML is considdered the new way to go in terms of customizable UI's. The sentence itself hints at the fact that it is not possible to query Qt for the results of the evaluation of the style sheet, so you can not use that in your custom rendering. The same holds for custom widgets.

                    And vinb: no need to retreat quietly :-) I am no oracle either.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MikeHammond
                      wrote on last edited by
                      #12

                      Thank you, Andre. (I walked through this code, but I was hoping that I'd missed something simple.)

                      One other thing: another (not as good) way to accomplish what I wanted to do is to set properties in the widget to set the values you wish, and then set those properties in a stylesheet. Using the above example, I could create holeBackgroundColor and holeColor QBrush properties for the QSwissCheese widget, and then use the following style sheet:
                      @QSwissCheese
                      {
                      background-color: rgb(255, 240, 0);
                      color: rgb(0, 0, 0);
                      qproperty-holeBackgroundColor: rgb(0, 0, 0);
                      qproperty-holeColor: rgb(255, 255, 255);
                      }@

                      Unfortunately, by doing so, these properties can then be set in an IDE (like Qt Designer), and the style sheet will override them, leading to potential confusion. I'm also not sure if you can set more complicated style sheet property types, like Box Colors or Box Lengths.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        vinb
                        wrote on last edited by
                        #13

                        @Andre, thanks for those words. :)
                        But i know for sure, that your knowledge of Qt is much greater then mine.

                        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