📄 qgroupbox.cpp
字号:
return QWidget::event(e);}/*!\reimp */void QGroupBox::childEvent(QChildEvent *c){ Q_D(QGroupBox); if (c->type() != QEvent::ChildAdded || !c->child()->isWidgetType()) return; QWidget *w = (QWidget*)c->child(); if (d->checkable) { if (d->checked) { if (!w->testAttribute(Qt::WA_ForceDisabled)) w->setEnabled(true); } else { if (w->isEnabled()) { w->setEnabled(false); w->setAttribute(Qt::WA_ForceDisabled, false); } } }}/*! \internal This private slot finds a widget in this group box that can accept focus, and gives the focus to that widget.*/void QGroupBoxPrivate::_q_fixFocus(){ Q_Q(QGroupBox); QWidget *fw = q->focusWidget(); if (!fw) { QWidget * best = 0; QWidget * candidate = 0; QWidget * w = q; while ((w = w->nextInFocusChain()) != q) { if (q->isAncestorOf(w) && (w->focusPolicy() & Qt::TabFocus) == Qt::TabFocus && w->isVisibleTo(q)) { if (!best && qobject_cast<QRadioButton*>(w) && ((QRadioButton*)w)->isChecked()) // we prefer a checked radio button or a widget that // already has focus, if there is one best = w; else if (!candidate) // but we'll accept anything that takes focus candidate = w; } } if (best) fw = best; else if (candidate) fw = candidate; } if (fw) fw->setFocus();}/* Sets the right frame rect depending on the title.*/void QGroupBoxPrivate::calculateFrame(){ Q_Q(QGroupBox); QStyleOptionGroupBox box = getStyleOption(); QRect contentsRect = q->style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxContents, q); QRect frameRect = q->style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxFrame, q); q->setContentsMargins(contentsRect.left() - frameRect.left(), contentsRect.top() - frameRect.top(), frameRect.right() - contentsRect.right(), frameRect.bottom() - contentsRect.bottom());}/*! \reimp */void QGroupBox::focusInEvent(QFocusEvent *){ // note no call to super Q_D(QGroupBox); if (focusPolicy() == Qt::NoFocus) { d->_q_fixFocus(); } else { QStyleOptionGroupBox box = d->getStyleOption(); QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this) | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel, this); update(rect); }}/*! \reimp*/QSize QGroupBox::minimumSizeHint() const{ Q_D(const QGroupBox); QStyleOptionGroupBox option = d->getStyleOption(); int baseWidth = fontMetrics().width(d->title + QLatin1Char(' ')); int baseHeight = fontMetrics().height(); if (d->checkable) { baseWidth += style()->pixelMetric(QStyle::PM_IndicatorWidth); baseWidth += style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing); baseHeight = qMax(baseHeight, style()->pixelMetric(QStyle::PM_IndicatorHeight)); } QSize size = QWidget::minimumSizeHint().expandedTo(QSize(baseWidth, baseHeight)); return style()->sizeFromContents(QStyle::CT_GroupBox, &option, size, this);}/*! \property QGroupBox::flat \brief whether the group box is painted flat or has a frame By default a group box has a surrounding frame, with the title being placed on the upper frame line. In flat mode the right, left and bottom frame lines are omitted, and only the thin line at the top is drawn. \sa title*/bool QGroupBox::isFlat() const{ Q_D(const QGroupBox); return d->flat;}void QGroupBox::setFlat(bool b){ Q_D(QGroupBox); if (d->flat == b) return; d->flat = b; updateGeometry(); update();}/*! \property QGroupBox::checkable \brief Whether the group box has a checkbox in its title. If this property is true, the group box has a checkbox. If the checkbox is checked (which is the default), the group box's children are enabled. setCheckable() controls whether or not the group box has a checkbox, and isCheckable() controls whether the checkbox is checked or not.*/void QGroupBox::setCheckable(bool checkable){ Q_D(QGroupBox); bool wasCheckable = d->checkable; d->checkable = checkable; if (checkable) { setChecked(true); if (!wasCheckable) { setFocusPolicy(Qt::StrongFocus); d->_q_setChildrenEnabled(true); updateGeometry(); } } else { if (wasCheckable) { setFocusPolicy(Qt::NoFocus); d->_q_setChildrenEnabled(true); updateGeometry(); } d->_q_setChildrenEnabled(true); } if (wasCheckable != checkable) update();}bool QGroupBox::isCheckable() const{ Q_D(const QGroupBox); return d->checkable;}bool QGroupBox::isChecked() const{ Q_D(const QGroupBox); return d->checkable && d->checked;}/*! \fn void QGroupBox::toggled(bool on) If the group box has a check box (see \l isCheckable()) this signal is emitted when the check box is toggled. \a on is true if the check box is checked; otherwise it is false.*//*! \property QGroupBox::checked \brief Whether the group box's checkbox is checked. If the group box has a check box (see \l isCheckable()), and the check box is checked, the group box's children are enabled. If the checkbox is unchecked the children are disabled.*/void QGroupBox::setChecked(bool b){ Q_D(QGroupBox); if (d->checkable) { if (d->checked != b) update(); bool wasToggled = (b != d->checked); d->checked = b; if (wasToggled) { d->_q_setChildrenEnabled(b); emit toggled(b); } }}/* sets all children of the group box except the qt_groupbox_checkbox to either disabled/enabled*/void QGroupBoxPrivate::_q_setChildrenEnabled(bool b){ Q_Q(QGroupBox); QObjectList childs = q->children(); if (childs.isEmpty()) return; for (int i = 0; i < childs.size(); ++i) { QObject *o = childs.at(i); if (o->isWidgetType()) { QWidget *w = static_cast<QWidget *>(o); if (b) { if (!w->testAttribute(Qt::WA_ForceDisabled)) w->setEnabled(true); } else { if (w->isEnabled()) { w->setEnabled(false); w->setAttribute(Qt::WA_ForceDisabled, false); } } } }}/*! \reimp */void QGroupBox::changeEvent(QEvent *ev){ Q_D(QGroupBox); if(ev->type() == QEvent::EnabledChange) { if (d->checkable && isEnabled()) { // we are being enabled - disable children if (!d->checked) d->_q_setChildrenEnabled(false); } } else if(ev->type() == QEvent::FontChange || ev->type() == QEvent::StyleChange) { updateGeometry(); d->calculateFrame(); } QWidget::changeEvent(ev);}/*! \reimp */void QGroupBox::mousePressEvent(QMouseEvent *event){ Q_D(QGroupBox); QStyleOptionGroupBox box = d->getStyleOption(); d->pressedControl = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this); if (d->pressedControl & (QStyle::SC_GroupBoxCheckBox | QStyle::SC_GroupBoxLabel)) update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this));}/*! \reimp */void QGroupBox::mouseMoveEvent(QMouseEvent *event){ Q_D(QGroupBox); QStyleOptionGroupBox box = d->getStyleOption(); QStyle::SubControl pressed = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this); if (d->pressedControl == QStyle::SC_GroupBoxCheckBox && d->pressedControl != pressed) update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this));}/*! \reimp */void QGroupBox::mouseReleaseEvent(QMouseEvent *){ Q_D(QGroupBox); QStyleOptionGroupBox box = d->getStyleOption(); bool toggle = (d->pressedControl == QStyle::SC_GroupBoxLabel || d->pressedControl == QStyle::SC_GroupBoxCheckBox); d->pressedControl = QStyle::SC_None; if (toggle) setChecked(!d->checked);}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QGroupBox::QGroupBox(QWidget *parent, const char *name) : QWidget(*new QGroupBoxPrivate, parent, 0){ Q_D(QGroupBox); setObjectName(QString::fromAscii(name)); d->init();}/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QGroupBox::QGroupBox(const QString &title, QWidget *parent, const char *name) : QWidget(*new QGroupBoxPrivate, parent, 0){ Q_D(QGroupBox); setObjectName(QString::fromAscii(name)); d->init(); setTitle(title);}#endif // QT3_SUPPORT#include "moc_qgroupbox.cpp"#endif //QT_NO_GROUPBOX
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -