63void QGroupBox::initStyleOption(QStyleOptionGroupBox *option)
const
69 option->initFrom(
this);
70 option->text = d->title;
71 option->lineWidth = 1;
72 option->midLineWidth = 0;
73 option->textAlignment = Qt::Alignment(d->align);
74 option->activeSubControls |= d->pressedControl;
75 option->subControls = QStyle::SC_GroupBoxFrame;
77 option->state.setFlag(QStyle::State_MouseOver, d->hover);
79 option->features |= QStyleOptionFrame::Flat;
82 option->subControls |= QStyle::SC_GroupBoxCheckBox;
83 option->state |= (d->checked ? QStyle::State_On : QStyle::State_Off);
84 if ((d->pressedControl == QStyle::SC_GroupBoxCheckBox
85 || d->pressedControl == QStyle::SC_GroupBoxLabel) && (d->hover || d->overCheckBox))
86 option->state |= QStyle::State_Sunken;
89 if (!option->palette.isBrushSet(isEnabled() ? QPalette::Active :
90 QPalette::Disabled, QPalette::WindowText))
91 option->textColor = QColor(style()->styleHint(QStyle::SH_GroupBox_TextLabelColor,
94 if (!d->title.isEmpty())
95 option->subControls |= QStyle::SC_GroupBoxLabel;
192void QGroupBox::setTitle(
const QString &title)
195 if (d->title == title)
198#ifndef QT_NO_SHORTCUT
199 releaseShortcut(d->shortcutId);
200 d->shortcutId = grabShortcut(QKeySequence::mnemonic(title));
206#if QT_CONFIG(accessibility)
207 QAccessibleEvent event(
this, QAccessible::NameChanged);
208 QAccessible::updateAccessibility(&event);
279void QGroupBox::paintEvent(QPaintEvent *)
281 QStylePainter paint(
this);
282 QStyleOptionGroupBox option;
283 initStyleOption(&option);
284 paint.drawComplexControl(QStyle::CC_GroupBox, option);
288bool QGroupBox::event(QEvent *e)
291#ifndef QT_NO_SHORTCUT
292 if (e->type() == QEvent::Shortcut) {
293 QShortcutEvent *se =
static_cast<QShortcutEvent *>(e);
294 if (se->shortcutId() == d->shortcutId) {
295 if (!isCheckable()) {
296 d->_q_fixFocus(Qt::ShortcutFocusReason);
299 setFocus(Qt::ShortcutFocusReason);
305 QStyleOptionGroupBox box;
306 initStyleOption(&box);
308 case QEvent::HoverEnter:
309 case QEvent::HoverMove: {
310 QStyle::SubControl control = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
311 static_cast<QHoverEvent *>(e)->position().toPoint(),
313 bool oldHover = d->hover;
314 d->hover = d->checkable && (control == QStyle::SC_GroupBoxLabel || control == QStyle::SC_GroupBoxCheckBox);
315 if (oldHover != d->hover) {
316 QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox,
this)
317 | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel,
this);
322 case QEvent::HoverLeave:
325 QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox,
this)
326 | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel,
this);
330 case QEvent::KeyPress: {
331 QKeyEvent *k =
static_cast<QKeyEvent*>(e);
332 if (d->shouldHandleKeyEvent(k)) {
333 d->pressedControl = QStyle::SC_GroupBoxCheckBox;
334 update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox,
this));
339 case QEvent::KeyRelease: {
340 QKeyEvent *k =
static_cast<QKeyEvent*>(e);
341 if (d->shouldHandleKeyEvent(k)) {
342 bool toggle = (d->pressedControl == QStyle::SC_GroupBoxLabel
343 || d->pressedControl == QStyle::SC_GroupBoxCheckBox);
344 d->pressedControl = QStyle::SC_None;
354 return QWidget::event(e);
358void QGroupBox::childEvent(QChildEvent *c)
362
363
364
365 if (!(c->added() || c->polished()) || !c->child()->isWidgetType())
367 QWidget *w =
static_cast<QWidget*>(c->child());
372 if (!w->testAttribute(Qt::WA_ForceDisabled))
375 if (w->isEnabled()) {
376 w->setEnabled(
false);
377 w->setAttribute(Qt::WA_ForceDisabled,
false);
428 QStyleOptionGroupBox box;
429 q->initStyleOption(&box);
430 QRect contentsRect = q->style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxContents, q);
431 q->setContentsMargins(contentsRect.left() - box.rect.left(), contentsRect.top() - box.rect.top(),
432 box.rect.right() - contentsRect.right(), box.rect.bottom() - contentsRect.bottom());
433 setLayoutItemMargins(QStyle::SE_GroupBoxLayoutItem, &box);
452QSize QGroupBox::minimumSizeHint()
const
454 Q_D(
const QGroupBox);
455 QStyleOptionGroupBox option;
456 initStyleOption(&option);
458 QFontMetrics metrics(fontMetrics());
460 int baseWidth = metrics.horizontalAdvance(d->title) + metrics.horizontalAdvance(u' ');
461 int baseHeight = metrics.height();
463 baseWidth += style()->pixelMetric(QStyle::PM_IndicatorWidth, &option,
this);
464 baseWidth += style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &option,
this);
465 baseHeight = qMax(baseHeight, style()->pixelMetric(QStyle::PM_IndicatorHeight, &option,
this));
468 QSize size = style()->sizeFromContents(QStyle::CT_GroupBox, &option, QSize(baseWidth, baseHeight),
this);
469 return size.expandedTo(QWidget::minimumSizeHint());
522void QGroupBox::setCheckable(
bool checkable)
526 bool wasCheckable = d->checkable;
527 d->checkable = checkable;
532 setFocusPolicy(Qt::StrongFocus);
533 d->_q_setChildrenEnabled(
true);
538 setFocusPolicy(Qt::NoFocus);
539 d->_q_setChildrenEnabled(
true);
542 d->_q_setChildrenEnabled(
true);
545 if (wasCheckable != checkable) {
648void QGroupBox::changeEvent(QEvent *ev)
651 if (ev->type() == QEvent::EnabledChange) {
652 if (d->checkable && isEnabled()) {
655 d->_q_setChildrenEnabled(
false);
657 }
else if (ev->type() == QEvent::FontChange
659 || ev->type() == QEvent::MacSizeChange
661 || ev->type() == QEvent::StyleChange) {
664 QWidget::changeEvent(ev);
668void QGroupBox::mousePressEvent(QMouseEvent *event)
670 if (event->button() != Qt::LeftButton) {
676 QStyleOptionGroupBox box;
677 initStyleOption(&box);
678 d->pressedControl = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
679 event->position().toPoint(),
this);
680 if (d->checkable && (d->pressedControl & (QStyle::SC_GroupBoxCheckBox | QStyle::SC_GroupBoxLabel))) {
681 d->overCheckBox =
true;
682 update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox,
this));
689void QGroupBox::mouseMoveEvent(QMouseEvent *event)
692 QStyleOptionGroupBox box;
693 initStyleOption(&box);
694 QStyle::SubControl pressed = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
695 event->position().toPoint(),
this);
696 bool oldOverCheckBox = d->overCheckBox;
697 d->overCheckBox = (pressed == QStyle::SC_GroupBoxCheckBox || pressed == QStyle::SC_GroupBoxLabel);
698 if (d->checkable && (d->pressedControl == QStyle::SC_GroupBoxCheckBox || d->pressedControl == QStyle::SC_GroupBoxLabel)
699 && (d->overCheckBox != oldOverCheckBox))
700 update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox,
this));
706void QGroupBox::mouseReleaseEvent(QMouseEvent *event)
708 if (event->button() != Qt::LeftButton) {
714 if (!d->overCheckBox) {
718 QStyleOptionGroupBox box;
719 initStyleOption(&box);
720 QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
721 event->position().toPoint(),
this);
722 bool toggle = d->checkable && (released == QStyle::SC_GroupBoxLabel
723 || released == QStyle::SC_GroupBoxCheckBox);
724 d->pressedControl = QStyle::SC_None;
725 d->overCheckBox =
false;
728 else if (d->checkable)
729 update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox,
this));
735 Q_Q(
const QGroupBox);
737 if (!q->isEnabled() || !q->isCheckable() || keyEvent->isAutoRepeat())
740 const QList<Qt::Key> buttonPressKeys = QGuiApplicationPrivate::platformTheme()
741 ->themeHint(QPlatformTheme::ButtonPressKeys)
742 .value<QList<Qt::Key>>();
743 return buttonPressKeys.contains(keyEvent->key());