📄 qgroupbox.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qgroupbox.h"#ifndef QT_NO_GROUPBOX#include "qapplication.h"#include "qbitmap.h"#include "qdrawutil.h"#include "qevent.h"#include "qlayout.h"#include "qradiobutton.h"#include "qstyle.h"#include "qstyleoption.h"#include "qstylepainter.h"#ifndef QT_NO_ACCESSIBILITY#include "qaccessible.h"#endif#include <private/qwidget_p.h>class QGroupBoxPrivate : public QWidgetPrivate{ Q_DECLARE_PUBLIC(QGroupBox)public: void skip(); void init(); void calculateFrame(); QString title; int align;#ifndef QT_NO_SHORTCUT int shortcutId;#endif void _q_fixFocus(); void _q_setChildrenEnabled(bool b); bool flat; bool checkable; bool checked; bool hover; QStyle::SubControl pressedControl; QStyleOptionGroupBox getStyleOption() const;};QStyleOptionGroupBox QGroupBoxPrivate::getStyleOption() const{ Q_Q(const QGroupBox); QStyleOptionGroupBox option; option.init(q); option.text = title; option.lineWidth = 1; option.midLineWidth = 0; option.textAlignment = Qt::Alignment(align); option.activeSubControls |= pressedControl; option.subControls = QStyle::SC_GroupBoxFrame; if (hover) option.state |= QStyle::State_MouseOver; else option.state &= ~QStyle::State_MouseOver; if (flat) option.features |= QStyleOptionFrameV2::Flat; if (checkable) { option.subControls |= QStyle::SC_GroupBoxCheckBox; option.state |= (checked ? QStyle::State_On : QStyle::State_Off); if (pressedControl == QStyle::SC_GroupBoxCheckBox || pressedControl == QStyle::SC_GroupBoxLabel) option.state |= QStyle::State_Sunken; } if (!q->testAttribute(Qt::WA_SetPalette)) option.textColor = QColor(q->style()->styleHint(QStyle::SH_GroupBox_TextLabelColor, &option, q)); if (!title.isEmpty()) option.subControls |= QStyle::SC_GroupBoxLabel; return option;}/*! \class QGroupBox qgroupbox.h \brief The QGroupBox widget provides a group box frame with a title. \ingroup organizers \ingroup geomanagement \ingroup appearance \mainclass A group box provides a frame, a title and a keyboard shortcut, and displays various other widgets inside itself. The title is on top, the keyboard shortcut moves keyboard focus to one of the group box's child widgets. QGroupBox also lets you set the \l title (normally set in the constructor) and the title's alignment(). If setCheckable(true) is called then the group box is isCheckable(), and it can be setChecked(). Checkable group boxes child widgets are enabled or disabled depending on whether or not the group box is isChecked(). To minimize space consumption, you can remove the right, left and bottom edges of the frame with setFlat(). \inlineimage plastique-groupbox.png Screenshot in Plastique style \inlineimage windows-groupbox.png Screenshot in Windows style \sa QButtonGroup*//*! Constructs a group box widget with no title and parent \a parent.*/QGroupBox::QGroupBox(QWidget *parent) : QWidget(*new QGroupBoxPrivate, parent, 0){ Q_D(QGroupBox); d->init();}/*! Constructs a group box with the title \a title and parent \a parent.*/QGroupBox::QGroupBox(const QString &title, QWidget *parent) : QWidget(*new QGroupBoxPrivate, parent, 0){ Q_D(QGroupBox); d->init(); setTitle(title);}/*! Destroys the group box.*/QGroupBox::~QGroupBox(){}void QGroupBoxPrivate::init(){ align = Qt::AlignLeft;#ifndef QT_NO_SHORTCUT shortcutId = 0;#endif flat = false; checkable = false; checked = true; hover = false; pressedControl = QStyle::SC_None; calculateFrame();}void QGroupBox::setTitle(const QString &title){ Q_D(QGroupBox); if (d->title == title) // no change return; d->title = title;#ifndef QT_NO_SHORTCUT releaseShortcut(d->shortcutId); d->shortcutId = grabShortcut(QKeySequence::mnemonic(title));#endif d->calculateFrame(); update(); updateGeometry();#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::NameChanged);#endif}/*! \property QGroupBox::title \brief the group box title text. The group box title text will have a focus-change keyboard shortcut if the title contains \&, followed by a letter. \code g->setTitle("&User information"); \endcode This produces "\underline{U}ser information"; \key Alt+U moves the keyboard focus to the group box. There is no default title text.*/QString QGroupBox::title() const{ Q_D(const QGroupBox); return d->title;}/*! \property QGroupBox::alignment \brief the alignment of the group box title. The title is always placed on the upper frame line. The horizontal alignment can be specified by the alignment parameter. The alignment is one of the following flags: \list \i Qt::AlignLeft aligns the title text to the left. \i Qt::AlignRight aligns the title text to the right. \i Qt::AlignHCenter aligns the title text centered. \endlist The default alignment is Qt::AlignLeft. \sa Qt::Alignment*/Qt::Alignment QGroupBox::alignment() const{ Q_D(const QGroupBox); return QFlag(d->align);}void QGroupBox::setAlignment(int alignment){ Q_D(QGroupBox); d->align = alignment; updateGeometry(); update();}/*! \reimp*/void QGroupBox::resizeEvent(QResizeEvent *e){ QWidget::resizeEvent(e); updateGeometry();}/*! \reimp*/void QGroupBox::paintEvent(QPaintEvent *){ Q_D(QGroupBox); QStylePainter paint(this); paint.drawComplexControl(QStyle::CC_GroupBox, d->getStyleOption());}/*! \reimp */bool QGroupBox::event(QEvent *e){ Q_D(QGroupBox);#ifndef QT_NO_SHORTCUT if (e->type() == QEvent::Shortcut) { QShortcutEvent *se = static_cast<QShortcutEvent *>(e); if (se->shortcutId() == d->shortcutId) { if (!isCheckable()) { d->_q_fixFocus(); } else { setChecked(!d->checked); setFocus(Qt::ShortcutFocusReason); } return true; } }#endif QStyleOptionGroupBox box = d->getStyleOption(); switch (e->type()) { case QEvent::HoverEnter: case QEvent::HoverMove: { QStyle::SubControl control = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, static_cast<QHoverEvent *>(e)->pos(), this); bool oldHover = d->hover; d->hover = d->checkable && (control == QStyle::SC_GroupBoxLabel || control == QStyle::SC_GroupBoxCheckBox); if (oldHover != d->hover) { QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this) | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel, this); update(rect); } return true; } case QEvent::HoverLeave: d->hover = false; if (d->checkable) { QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this) | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel, this); update(rect); } return true; case QEvent::KeyPress: { QKeyEvent *k = static_cast<QKeyEvent*>(e); if (!k->isAutoRepeat() && (k->key() == Qt::Key_Select || k->key() == Qt::Key_Space)) { d->pressedControl = QStyle::SC_GroupBoxCheckBox; update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this)); return true; } break; } case QEvent::KeyRelease: { QKeyEvent *k = static_cast<QKeyEvent*>(e); if (!k->isAutoRepeat() && (k->key() == Qt::Key_Select || k->key() == Qt::Key_Space)) { bool toggle = (d->pressedControl == QStyle::SC_GroupBoxLabel || d->pressedControl == QStyle::SC_GroupBoxCheckBox); d->pressedControl = QStyle::SC_None; if (toggle) setChecked(!d->checked); return true; } break; } default: break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -