diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-07-21 16:31:29 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-07-22 16:17:49 +0200 |
commit | 188d739400e10fc8571bbf2ec86d5cd338b04a5d (patch) | |
tree | 5eefe4a54a537c118168b27b069dc48c99389596 /src/widgets | |
parent | 29017f1395b1bc52e60760fa58c92f6fa4ee4f60 (diff) |
Sync default action when checking tool button programmatically
QAbstractButton::setChecked is not virtual, so QToolButton cannot
override to synchronize the default action's checked state. This resulted
in button and default action not being in sync when the checked state
of the button was changed programmatically, while changing the checked
state on the action kept the button in sync.
Connect to the button's own toggled signal instead to keep the state of
the default action in sync. Make it a unique connection to allow multiple
calls to setDefaultAction, which are used by QToolButton to keep the
button updated if properties of the default action change.
Add a test that confirms that button and action are synchronized both
ways, and that we only get single signal emissions when changing either
programmatically.
Fixes: QTBUG-95255
Pick-to: 6.2 6.1
Change-Id: I0e027faf1da763ef1878e46e85bfa70073c8bf82
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/widgets/qtoolbutton.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/widgets/widgets/qtoolbutton.cpp b/src/widgets/widgets/qtoolbutton.cpp index b9d31bd657..ee58797d3c 100644 --- a/src/widgets/widgets/qtoolbutton.cpp +++ b/src/widgets/widgets/qtoolbutton.cpp @@ -961,6 +961,12 @@ void QToolButton::setDefaultAction(QAction *action) } #endif setCheckable(action->isCheckable()); + if (action->isCheckable()) { + connect(this, &QAbstractButton::toggled, this, [this](bool checked) { + if (defaultAction()) + defaultAction()->setChecked(checked); + }, Qt::UniqueConnection); + } setChecked(action->isChecked()); setEnabled(action->isEnabled()); if (action->d_func()->fontSet) |