[SOLVED] from qaction to qpushbutton?



  • 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.



  • 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.


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



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



  • 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).



  • [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?



  • 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!)



  • 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...



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.