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] from qaction to qpushbutton?
QtWS25 Last Chance

[SOLVED] from qaction to qpushbutton?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 11.1k 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.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

    Hi all,
    is it possible to define a qpushbutton having a qaction? I guess it is, or it should be, but I'm unable to understand why (addAction does not make it). In my application I'd like to have actions represented as menu and buttons, even context menu, so I think using actions is the best way to keep all synchronized.

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

      You have stumbled on an unfortunate situation. QPushButton does not really work with QAction. Not directly, anyway. I think you have two options:

      • Use a QToolButton instead. QToolButton has a setDefaultAction method that you can use to set the action for the button.
      • Subclass QPushButton, and add a setAction() method that takes a QAction. Make the button take over the action's properties when a new action is set, and watch for the QAction::changed() signal) for the button too. Last, make clicking the button invoke the QAction::trigger() slot. It should not be too difficult to do.
      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        Nice idea, I implemented a button that can be configured using an action.

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

          Anyway, I'm just curious, is there a technical reason why qpushbutton cannot be associated to an action?

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

            Not that I know of, no. Personally, I really like the concept of QAtion. For me, they represent a way to keep the state around actions nicely together, like information on the availablity of an action, or the user-visible description of it. That makes sense in a lot of situations, and I think using actions in combination with pushbuttons is a sensible thing to do. On first sight, setAction would belong in QAbstractButton and be therefore available for all concrete buttons.

            Also: IMHO it is a loss that there is no such concept in QML (AFAIK).

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              [quote author="Andre" date="1317818402"]On first sight, setAction would belong in QAbstractButton and be therefore available for all concrete buttons.[/quote]

              Not really, as QRadioButton and QCheckBox also derive from QAbstractButton. And does it make sense to connect an action with a check box?

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                What is strange to me is that a lot of gui toolkits, SWT for instance, allow the binding of actions to buttons, so if there is no techical reason this is a lack of qt (not meant to offend qt engineers!)

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

                  Yes, that makes perfect sense. The checking of a checkbox is also an action, and, what's more, QAction already describes CheckState. Note that if you put a checkable QAction in a menu, it will behave exactly like a QCheckBox does. So to me, it makes a lot of sense to be able to use a QAction as the abstract description of an on/off state, and a QCheckBox as a visual representation of that description.

                  However, perhaps QAction is not perfect, and we really need something even more abstract and basic than that in the end. A look at the current QAction API gives me the feeling it is too big already, and it is not entirely clear what concept it describes. Why does it cater to having a checked state, but not some other value for instance? What's the difference? Sure, you can use setData for that, but still...

                  Edit:
                  BTW: perhaps this could be changed for Qt 5, if there is interest to do the work. I think the widget world still deserves attention. Some other pet-peeves of mine are the inconstisten use of QValidator and way QUndoStack is (not) used consistently...

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

                    I guess the problem here is not about how QAction is appropriate for a QPushButton, but if there is the need for a common "action" to use for a set of commands. I guess there is!
                    And maybe having a look at how other toolkits implement this (e.g., the command framework of RCP) could be a good starting point for a refactoring/implementation fot qt5.

                    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