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 subclass QStyleOptionButton?
Forum Update on Monday, May 27th 2025

How to subclass QStyleOptionButton?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 470 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.
  • S Offline
    S Offline
    schrute
    wrote on last edited by schrute
    #1

    I am working on implementing a custom control derived from QPushButton. There are some options that can not be configured using the default QStyleOptionButton, so I am looking to subclass QStyleOptionButton. Please let me know if you are aware of any docs that explain how to do this. I found a document about creating custom styles but it doesn't cover this aspect.

    1 Reply Last reply
    0
    • S schrute

      @Axel-Spoerl The goal is to have a button widget (derived from QPushButton) that supports multiple variants (6), fill colors (3) that are not otherwise available with Qt's default button and style options. I can achieve this without subclassing QStyleOption/QStyleOptionButton by querying the widget directly inside my style class but was curious if I can use style option to pass the required info.

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #6

      @schrute: Style options are used for data exchange between widgets and styles. The widget populates it with data that each style may interpret differently when doing its drawing work. So in the case at hand, a custom style is supposed to react specifically to a custom widget. It’s a question of style (haha) whether to query the widget directly or derive from QStyleOption. Deriving from QStyleOption has the advantage that information can be detached at a specific state without being modified when the widget’s state transitions in the meanwhile. For instance if it should be drawn differently while being hovered, the widget may no longer know at the point of direct query, that it once was under the mouse cursor. That’s where I’d draw the line: If conserving fluctuating states is crucial, derive from QStyleOption. If that’s not the case, call the widget directly.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Since it's a c++ class you can derive from it like for any other class.
        But it won't help you since QPushButton instantiates a QStyleOptionButton internally, not your class.
        I also don't see why deriving from QStyleOptionButton should be needed.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply
        0
        • Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #3

          If the goal is to style a QPushButtondifferntly or in a more complex manner than native Qt styles do, you might want to look into this. It demonstrates how to implement a custom style, offering a variety of options how to specifically style a widget.

          Software Engineer
          The Qt Company, Oslo

          S 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Since it's a c++ class you can derive from it like for any other class.
            But it won't help you since QPushButton instantiates a QStyleOptionButton internally, not your class.
            I also don't see why deriving from QStyleOptionButton should be needed.

            S Offline
            S Offline
            schrute
            wrote on last edited by
            #4

            @Christian-Ehrlicher I was planning to use the derived style option in my derived QPushButton's paintEvent.

            1 Reply Last reply
            0
            • Axel SpoerlA Axel Spoerl

              If the goal is to style a QPushButtondifferntly or in a more complex manner than native Qt styles do, you might want to look into this. It demonstrates how to implement a custom style, offering a variety of options how to specifically style a widget.

              S Offline
              S Offline
              schrute
              wrote on last edited by
              #5

              @Axel-Spoerl The goal is to have a button widget (derived from QPushButton) that supports multiple variants (6), fill colors (3) that are not otherwise available with Qt's default button and style options. I can achieve this without subclassing QStyleOption/QStyleOptionButton by querying the widget directly inside my style class but was curious if I can use style option to pass the required info.

              Axel SpoerlA 1 Reply Last reply
              0
              • S schrute

                @Axel-Spoerl The goal is to have a button widget (derived from QPushButton) that supports multiple variants (6), fill colors (3) that are not otherwise available with Qt's default button and style options. I can achieve this without subclassing QStyleOption/QStyleOptionButton by querying the widget directly inside my style class but was curious if I can use style option to pass the required info.

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #6

                @schrute: Style options are used for data exchange between widgets and styles. The widget populates it with data that each style may interpret differently when doing its drawing work. So in the case at hand, a custom style is supposed to react specifically to a custom widget. It’s a question of style (haha) whether to query the widget directly or derive from QStyleOption. Deriving from QStyleOption has the advantage that information can be detached at a specific state without being modified when the widget’s state transitions in the meanwhile. For instance if it should be drawn differently while being hovered, the widget may no longer know at the point of direct query, that it once was under the mouse cursor. That’s where I’d draw the line: If conserving fluctuating states is crucial, derive from QStyleOption. If that’s not the case, call the widget directly.

                Software Engineer
                The Qt Company, Oslo

                1 Reply Last reply
                1
                • S schrute has marked this topic as solved on
                • I ian28 referenced this topic on

                • Login

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