QToolButton: reimplement the fix for QTBUG-95255

The code in 188d739400 uses a connect() to
a lambda, passing UniqueConnection to avoid establishing the connection
more than once. The problem is that UniqueConnection does not work
with lambdas; it works only with "regular" PMFs to QObject subclasses.

Re-do the same fix, but without a connection: use the checkStateSet()
virtual from the base class that will notify us if setChecked() is
being called on the tool button, and from there synchronize the state
of the default action.

Change-Id: Id512812c562cd6d20bc1a489753b33c269919d32
Fixes: QTBUG-95255
Pick-to: 6.2 6.1
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2021-08-26 19:56:54 +02:00
parent 89cda52d66
commit c9830c2fb9
2 changed files with 10 additions and 7 deletions

View File

@ -961,12 +961,6 @@ 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)
@ -985,7 +979,15 @@ QAction *QToolButton::defaultAction() const
return d->defaultAction;
}
/*!
\reimp
*/
void QToolButton::checkStateSet()
{
Q_D(QToolButton);
if (d->defaultAction && d->defaultAction->isCheckable())
d->defaultAction->setChecked(isChecked());
}
/*!
\reimp

View File

@ -118,6 +118,7 @@ protected:
void changeEvent(QEvent *) override;
bool hitButton(const QPoint &pos) const override;
void checkStateSet() override;
void nextCheckState() override;
virtual void initStyleOption(QStyleOptionToolButton *option) const;