156#if QT_CONFIG(buttongroup)
158 return group->d_func()->buttonList;
163 QList<QAbstractButton *> candidates = parent->findChildren<QAbstractButton *>();
165 auto isNoMemberOfMyAutoExclusiveGroup = [](QAbstractButton *candidate) {
166 return !candidate->autoExclusive()
167#if QT_CONFIG(buttongroup)
168 || candidate->group()
172 candidates.removeIf(isNoMemberOfMyAutoExclusiveGroup);
198#if QT_CONFIG(buttongroup)
199 Q_Q(QAbstractButton);
201 QAbstractButton *previous = group->d_func()->checkedButton;
202 group->d_func()->checkedButton = q;
203 if (group->d_func()->exclusive && previous && previous != q)
204 previous->nextCheckState();
208 if (QAbstractButton *b = queryCheckedButton())
209 b->setChecked(
false);
215 const QList<QAbstractButton *> buttonList = queryButtonList();
216#if QT_CONFIG(buttongroup)
217 bool exclusive = group ? group->d_func()->exclusive : autoExclusive;
219 bool exclusive = autoExclusive;
221 QWidget *f = QApplication::focusWidget();
222 QAbstractButton *fb = qobject_cast<QAbstractButton *>(f);
223 if (!fb || !buttonList.contains(fb))
226 QAbstractButton *candidate =
nullptr;
228 QRect target = f->rect().translated(f->mapToGlobal(QPoint(0,0)));
229 QPoint goal = target.center();
230 uint focus_flag = qt_tab_all_widgets() ? Qt::TabFocus : Qt::StrongFocus;
232 for (QAbstractButton *button : buttonList) {
233 if (button != f && button->window() == f->window() && button->isEnabled() && !button->isHidden() &&
234 (exclusive || (button->focusPolicy() & focus_flag) == focus_flag)) {
235 QRect buttonRect = button->rect().translated(button->mapToGlobal(QPoint(0,0)));
236 QPoint p = buttonRect.center();
242 if ((buttonRect.x() < target.right() && target.x() < buttonRect.right())
243 && (key == Qt::Key_Up || key == Qt::Key_Down)) {
245 score = (qAbs(p.y() - goal.y()) << 16) + qAbs(p.x() - goal.x());
246 }
else if ((buttonRect.y() < target.bottom() && target.y() < buttonRect.bottom())
247 && (key == Qt::Key_Left || key == Qt::Key_Right) ) {
249 score = (qAbs(p.x() - goal.x()) << 16) + qAbs(p.y() - goal.y());
251 score = (1 << 30) + (p.y() - goal.y()) * (p.y() - goal.y()) + (p.x() - goal.x()) * (p.x() - goal.x());
254 if (score > bestScore && candidate)
259 if (p.y() < goal.y()) {
265 if (p.y() > goal.y()) {
271 if (p.x() < goal.x()) {
277 if (p.x() > goal.x()) {
288 && fb->d_func()->checked
289 && candidate->d_func()->checkable)
293 if (key == Qt::Key_Up || key == Qt::Key_Left)
294 candidate->setFocus(Qt::BacktabFocusReason);
296 candidate->setFocus(Qt::TabFocusReason);
302 Q_Q(QAbstractButton);
303#if QT_CONFIG(buttongroup)
304 const bool exclusive = group ? group->d_func()->exclusive : autoExclusive;
306 const bool exclusive = autoExclusive;
311 const QList<QAbstractButton *> buttonList = queryButtonList();
312 for (QAbstractButton *b : buttonList) {
313 if (!b->isCheckable())
315 b->setFocusPolicy((Qt::FocusPolicy) ((b == q || !q->isCheckable())
316 ? (b->focusPolicy() | Qt::TabFocus)
317 : (b->focusPolicy() & ~Qt::TabFocus)));
323 Q_Q(QAbstractButton);
327 q->setFocusPolicy(Qt::FocusPolicy(q->style()->styleHint(QStyle::SH_Button_FocusPolicy,
329 q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, controlType));
330 q->setAttribute(Qt::WA_WState_OwnSizePolicy,
false);
331 q->setForegroundRole(QPalette::ButtonText);
332 q->setBackgroundRole(QPalette::Button);
488void QAbstractButton::setText(
const QString &text)
490 Q_D(QAbstractButton);
494#ifndef QT_NO_SHORTCUT
495 QKeySequence newMnemonic = QKeySequence::mnemonic(text);
496 setShortcut(newMnemonic);
498 d->sizeHint = QSize();
501#if QT_CONFIG(accessibility)
502 QAccessibleEvent event(
this, QAccessible::NameChanged);
503 QAccessible::updateAccessibility(&event);
590void QAbstractButton::setChecked(
bool checked)
592 Q_D(QAbstractButton);
593 if (!d->checkable || d->checked == checked) {
594 if (!d->blockRefresh)
599 if (!checked && d->queryCheckedButton() ==
this) {
601#if QT_CONFIG(buttongroup)
602 if (d->group ? d->group->d_func()->exclusive : d->autoExclusive)
605 d->group->d_func()->detectCheckedButton();
607 if (d->autoExclusive)
612 QPointer<QAbstractButton> guard(
this);
614 d->checked = checked;
615 if (!d->blockRefresh)
619 if (guard && checked)
622 d->emitToggled(checked);
624#if QT_CONFIG(accessibility)
626 QAccessible::State s;
628 QAccessibleStateChangeEvent event(
this, s);
629 QAccessible::updateAccessibility(&event);
682void QAbstractButton::setAutoRepeat(
bool autoRepeat)
684 Q_D(QAbstractButton);
685 if (d->autoRepeat == autoRepeat)
687 d->autoRepeat = autoRepeat;
688 if (d->autoRepeat && d->down)
689 d->repeatTimer.start(d->autoRepeatDelay,
this);
691 d->repeatTimer.stop();
806void QAbstractButton::animateClick()
810 Q_D(QAbstractButton);
811 if (d->checkable && focusPolicy() & Qt::ClickFocus)
815 if (!d->animateTimer.isActive())
817 d->animateTimer.start(100,
this);
899bool QAbstractButton::event(QEvent *e)
905 case QEvent::TabletPress:
906 case QEvent::TabletRelease:
907 case QEvent::TabletMove:
908 case QEvent::MouseButtonPress:
909 case QEvent::MouseButtonRelease:
910 case QEvent::MouseButtonDblClick:
911 case QEvent::MouseMove:
912 case QEvent::HoverMove:
913 case QEvent::HoverEnter:
914 case QEvent::HoverLeave:
915 case QEvent::ContextMenu:
922#ifndef QT_NO_SHORTCUT
923 if (e->type() == QEvent::Shortcut) {
924 Q_D(QAbstractButton);
925 QShortcutEvent *se =
static_cast<QShortcutEvent *>(e);
926 if (d->shortcutId != se->shortcutId())
928 if (!se->isAmbiguous()) {
929 if (!d->animateTimer.isActive())
932 if (focusPolicy() != Qt::NoFocus)
933 setFocus(Qt::ShortcutFocusReason);
934 window()->setAttribute(Qt::WA_KeyboardFocusChange);
939 return QWidget::event(e);
1013void QAbstractButton::keyPressEvent(QKeyEvent *e)
1015 Q_D(QAbstractButton);
1018 const auto key = e->key();
1019 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
1020 ->themeHint(QPlatformTheme::ButtonPressKeys)
1021 .value<QList<Qt::Key>>();
1022 if (buttonPressKeys.contains(key) && !e->isAutoRepeat()) {
1035 case Qt::Key_Down: {
1036 QWidget *pw = parentWidget();
1037 if (d->autoExclusive
1038#if QT_CONFIG(buttongroup)
1041#if QT_CONFIG(itemviews)
1042 || (pw && qobject_cast<QAbstractItemView *>(pw->parentWidget()))
1049 d->moveFocus(e->key());
1054 QWidget *w = pw ? pw :
this;
1055 bool reverse = (w->layoutDirection() == Qt::RightToLeft);
1056 if ((e->key() == Qt::Key_Left && !reverse)
1057 || (e->key() == Qt::Key_Right && reverse)) {
1060 focusNextPrevChild(next);
1065#ifndef QT_NO_SHORTCUT
1066 if (e->matches(QKeySequence::Cancel) && d->down) {
1078void QAbstractButton::keyReleaseEvent(QKeyEvent *e)
1080 Q_D(QAbstractButton);
1082 if (!e->isAutoRepeat())
1083 d->repeatTimer.stop();
1085 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
1086 ->themeHint(QPlatformTheme::ButtonPressKeys)
1087 .value<QList<Qt::Key>>();
1088 if (buttonPressKeys.contains(e->key()) && !e->isAutoRepeat() && d->down) {
1098void QAbstractButton::timerEvent(QTimerEvent *e)
1100 Q_D(QAbstractButton);
1101 if (e->timerId() == d->repeatTimer.timerId()) {
1102 d->repeatTimer.start(d->autoRepeatInterval,
this);
1104 QPointer<QAbstractButton> guard(
this);
1113 }
else if (e->timerId() == d->animateTimer.timerId()) {
1114 d->animateTimer.stop();