0

Say I have a QToolButton with a delayed action showing a menu. Said menu contains a number of checkable actions. In this particular case it's a button activating a zooming tool, and there are 3 zooming modi QActions available, organized in a QActionGroup.

I want that tool button being checked if any of the of the checkable actions within the menu is being checked. How can I do this in a concise and Qt-idiomatic way?

datenwolf
  • 159,371
  • 13
  • 185
  • 298

1 Answers1

1

I don't use QToolButton, but I do some voodoo w/ QAction's state being changed from various places. Every time QAction::toggled() is called, the connected slot needs to perform a check. I see two ways to do this.

1: The lighter way is to simply increment some member that keeps track of how many QActions under that QToolButton have been enabled. Increment when a QAction is checked. Decrement it when they are unchecked. If that counter is > 0, check your QToolButton. If it's 0, uncheck it. This means your slot will need to know the state, which you can do by casting sender() to QAction* within the slot.

2: If you have some type of mapping of QAction to QToolButton, you can iterate over them every time the slot is called. This seems to be the naive approach to me, and more of a hassle to keep up w/ in this scenario. I sometimes have to add QAction to a QMenu based on another QAction being triggered, so I've already got a similar mapping place.

kiss-o-matic
  • 1,111
  • 16
  • 32