156QList<QAbstractButton *>QAbstractButtonPrivate::queryButtonList()
const
158#if QT_CONFIG(buttongroup)
160 return group->d_func()->buttonList;
165 QList<QAbstractButton *> candidates = parent->findChildren<QAbstractButton *>();
167 auto isNoMemberOfMyAutoExclusiveGroup = [](QAbstractButton *candidate) {
168 return !candidate->autoExclusive()
169#if QT_CONFIG(buttongroup)
170 || candidate->group()
174 candidates.removeIf(isNoMemberOfMyAutoExclusiveGroup);
179QAbstractButton *QAbstractButtonPrivate::queryCheckedButton()
const
181#if QT_CONFIG(buttongroup)
183 return group->d_func()->checkedButton;
186 Q_Q(
const QAbstractButton);
187 const QList<QAbstractButton *> buttonList = queryButtonList();
188 if (!autoExclusive || buttonList.size() == 1)
191 for (QAbstractButton *b : buttonList) {
192 if (b->d_func()->checked && b != q)
195 return checked ?
const_cast<QAbstractButton *>(q) :
nullptr;
198void QAbstractButtonPrivate::notifyChecked()
200#if QT_CONFIG(buttongroup)
201 Q_Q(QAbstractButton);
203 QAbstractButton *previous = group->d_func()->checkedButton;
204 group->d_func()->checkedButton = q;
205 if (group->d_func()->exclusive && previous && previous != q)
206 previous->nextCheckState();
210 if (QAbstractButton *b = queryCheckedButton())
211 b->setChecked(
false);
215void QAbstractButtonPrivate::moveFocus(
int key)
217 const QList<QAbstractButton *> buttonList = queryButtonList();
218#if QT_CONFIG(buttongroup)
219 bool exclusive = group ? group->d_func()->exclusive : autoExclusive;
221 bool exclusive = autoExclusive;
223 QWidget *f = QApplication::focusWidget();
224 QAbstractButton *fb = qobject_cast<QAbstractButton *>(f);
225 if (!fb || !buttonList.contains(fb))
228 QAbstractButton *candidate =
nullptr;
230 QRect target = f->rect().translated(f->mapToGlobal(QPoint(0,0)));
231 QPoint goal = target.center();
232 uint focus_flag = qt_tab_all_widgets() ? Qt::TabFocus : Qt::StrongFocus;
234 for (QAbstractButton *button : buttonList) {
235 if (button != f && button->window() == f->window() && button->isEnabled() && !button->isHidden() &&
236 (exclusive || (button->focusPolicy() & focus_flag) == focus_flag)) {
237 QRect buttonRect = button->rect().translated(button->mapToGlobal(QPoint(0,0)));
238 QPoint p = buttonRect.center();
244 if ((buttonRect.x() < target.right() && target.x() < buttonRect.right())
245 && (key == Qt::Key_Up || key == Qt::Key_Down)) {
247 score = (qAbs(p.y() - goal.y()) << 16) + qAbs(p.x() - goal.x());
248 }
else if ((buttonRect.y() < target.bottom() && target.y() < buttonRect.bottom())
249 && (key == Qt::Key_Left || key == Qt::Key_Right) ) {
251 score = (qAbs(p.x() - goal.x()) << 16) + qAbs(p.y() - goal.y());
253 score = (1 << 30) + (p.y() - goal.y()) * (p.y() - goal.y()) + (p.x() - goal.x()) * (p.x() - goal.x());
256 if (score > bestScore && candidate)
261 if (p.y() < goal.y()) {
267 if (p.y() > goal.y()) {
273 if (p.x() < goal.x()) {
279 if (p.x() > goal.x()) {
290 && fb->d_func()->checked
291 && candidate->d_func()->checkable)
295 if (key == Qt::Key_Up || key == Qt::Key_Left)
296 candidate->setFocus(Qt::BacktabFocusReason);
298 candidate->setFocus(Qt::TabFocusReason);
302void QAbstractButtonPrivate::fixFocusPolicy()
304 Q_Q(QAbstractButton);
305#if QT_CONFIG(buttongroup)
306 const bool exclusive = group ? group->d_func()->exclusive : autoExclusive;
308 const bool exclusive = autoExclusive;
313 const QList<QAbstractButton *> buttonList = queryButtonList();
314 for (QAbstractButton *b : buttonList) {
315 if (!b->isCheckable())
317 b->setFocusPolicy((Qt::FocusPolicy) ((b == q || !q->isCheckable())
318 ? (b->focusPolicy() | Qt::TabFocus)
319 : (b->focusPolicy() & ~Qt::TabFocus)));
323void QAbstractButtonPrivate::init()
325 Q_Q(QAbstractButton);
329 q->setFocusPolicy(Qt::FocusPolicy(q->style()->styleHint(QStyle::SH_Button_FocusPolicy,
331 q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, controlType));
332 q->setAttribute(Qt::WA_WState_OwnSizePolicy,
false);
333 q->setForegroundRole(QPalette::ButtonText);
334 q->setBackgroundRole(QPalette::Button);
378void QAbstractButtonPrivate::emitClicked()
380 Q_Q(QAbstractButton);
381 QPointer<QAbstractButton> guard(q);
382 emit q->clicked(checked);
383#if QT_CONFIG(buttongroup)
384 if (guard && group) {
385 emit group->idClicked(group->id(q));
387 emit group->buttonClicked(q);
420void QAbstractButtonPrivate::emitToggled(
bool checked)
422 Q_Q(QAbstractButton);
423 QPointer<QAbstractButton> guard(q);
424 emit q->toggled(checked);
425#if QT_CONFIG(buttongroup)
426 if (guard && group) {
427 emit group->idToggled(group->id(q), checked);
429 emit group->buttonToggled(q, checked);
490void QAbstractButton::setText(
const QString &text)
492 Q_D(QAbstractButton);
496#ifndef QT_NO_SHORTCUT
497 QKeySequence newMnemonic = QKeySequence::mnemonic(text);
498 setShortcut(newMnemonic);
500 d->sizeHint = QSize();
503#if QT_CONFIG(accessibility)
504 QAccessibleEvent event(
this, QAccessible::NameChanged);
505 QAccessible::updateAccessibility(&event);
592void QAbstractButton::setChecked(
bool checked)
594 Q_D(QAbstractButton);
595 if (!d->checkable || d->checked == checked) {
596 if (!d->blockRefresh)
601 if (!checked && d->queryCheckedButton() ==
this) {
603#if QT_CONFIG(buttongroup)
604 if (d->group ? d->group->d_func()->exclusive : d->autoExclusive)
607 d->group->d_func()->detectCheckedButton();
609 if (d->autoExclusive)
614 QPointer<QAbstractButton> guard(
this);
616 d->checked = checked;
617 if (!d->blockRefresh)
621 if (guard && checked)
624 d->emitToggled(checked);
626#if QT_CONFIG(accessibility)
628 QAccessible::State s;
630 QAccessibleStateChangeEvent event(
this, s);
631 QAccessible::updateAccessibility(&event);
684void QAbstractButton::setAutoRepeat(
bool autoRepeat)
686 Q_D(QAbstractButton);
687 if (d->autoRepeat == autoRepeat)
689 d->autoRepeat = autoRepeat;
690 if (d->autoRepeat && d->down)
691 d->repeatTimer.start(d->autoRepeatDelay,
this);
693 d->repeatTimer.stop();
856void QAbstractButton::animateClick()
860 Q_D(QAbstractButton);
861 if (d->checkable && focusPolicy() & Qt::ClickFocus)
865 if (!d->animateTimer.isActive())
867 d->animateTimer.start(100,
this);
949bool QAbstractButton::event(QEvent *e)
955 case QEvent::TabletPress:
956 case QEvent::TabletRelease:
957 case QEvent::TabletMove:
958 case QEvent::MouseButtonPress:
959 case QEvent::MouseButtonRelease:
960 case QEvent::MouseButtonDblClick:
961 case QEvent::MouseMove:
962 case QEvent::HoverMove:
963 case QEvent::HoverEnter:
964 case QEvent::HoverLeave:
965 case QEvent::ContextMenu:
972#ifndef QT_NO_SHORTCUT
973 if (e->type() == QEvent::Shortcut) {
974 Q_D(QAbstractButton);
975 QShortcutEvent *se =
static_cast<QShortcutEvent *>(e);
976 if (d->shortcutId != se->shortcutId())
978 if (!se->isAmbiguous()) {
979 if (!d->animateTimer.isActive())
982 if (focusPolicy() != Qt::NoFocus)
983 setFocus(Qt::ShortcutFocusReason);
984 window()->setAttribute(Qt::WA_KeyboardFocusChange);
989 return QWidget::event(e);
1063void QAbstractButton::keyPressEvent(QKeyEvent *e)
1065 Q_D(QAbstractButton);
1068 const auto key = e->key();
1069 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
1070 ->themeHint(QPlatformTheme::ButtonPressKeys)
1071 .value<QList<Qt::Key>>();
1072 if (buttonPressKeys.contains(key) && !e->isAutoRepeat()) {
1085 case Qt::Key_Down: {
1086 QWidget *pw = parentWidget();
1087 if (d->autoExclusive
1088#if QT_CONFIG(buttongroup)
1091#if QT_CONFIG(itemviews)
1092 || (pw && qobject_cast<QAbstractItemView *>(pw->parentWidget()))
1099 d->moveFocus(e->key());
1104 QWidget *w = pw ? pw :
this;
1105 bool reverse = (w->layoutDirection() == Qt::RightToLeft);
1106 if ((e->key() == Qt::Key_Left && !reverse)
1107 || (e->key() == Qt::Key_Right && reverse)) {
1110 focusNextPrevChild(next);
1115#ifndef QT_NO_SHORTCUT
1116 if (e->matches(QKeySequence::Cancel) && d->down) {
1128void QAbstractButton::keyReleaseEvent(QKeyEvent *e)
1130 Q_D(QAbstractButton);
1132 if (!e->isAutoRepeat())
1133 d->repeatTimer.stop();
1135 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
1136 ->themeHint(QPlatformTheme::ButtonPressKeys)
1137 .value<QList<Qt::Key>>();
1138 if (buttonPressKeys.contains(e->key()) && !e->isAutoRepeat() && d->down) {
1148void QAbstractButton::timerEvent(QTimerEvent *e)
1150 Q_D(QAbstractButton);
1151 if (e->timerId() == d->repeatTimer.timerId()) {
1152 d->repeatTimer.start(d->autoRepeatInterval,
this);
1154 QPointer<QAbstractButton> guard(
this);
1163 }
else if (e->timerId() == d->animateTimer.timerId()) {
1164 d->animateTimer.stop();