📄 qstyleoption.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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 "qstyleoption.h"#include "qapplication.h"#ifdef Q_WS_MAC# include "private/qt_mac_p.h"# include "qmacstyle_mac.h"#endif#ifndef QT_NO_DEBUG#include <qdebug.h>#endif/*! \class QStyleOption \brief The QStyleOption class stores the parameters used by QStyle functions. \ingroup appearance QStyleOption and its subclasses contain all the information that QStyle functions need to draw a graphical element. For performance reasons, there are few member functions and the access to the member variables is direct (i.e., using the \c . or \c -> operator). This low-level feel makes the structures straightforward to use and emphasizes that these are simply parameters used by the style functions. The caller of a QStyle function usually creates QStyleOption objects on the stack. This combined with Qt's extensive use of \l{implicit sharing} for types such as QString, QPalette, and QColor ensures that no memory allocation needlessly takes place. The following code snippet shows how to use a specific QStyleOption subclass to paint a push button: \quotefromfile snippets/qstyleoption/main.cpp \skipto MyPushButton::paintEvent \printuntil /^\}/ In our example, the control is a QStyle::CE_PushButton, and according to the QStyle::drawControl() documentation the corresponding class is QStyleOptionButton. When reimplementing QStyle functions that take a QStyleOption parameter, you often need to cast the QStyleOption to a subclass. For safety, you can use qstyleoption_cast() to ensure that the pointer type is correct. For example: \quotefromfile snippets/qstyleoption/main.cpp \skipto MyStyle::drawPrimitive \printuntil /^\}/ The qstyleoption_cast() function will return 0 if the object to which \c option points is not of the correct type. For an example demonstrating how style options can be used, see the \l {widgets/styles}{Styles} example. \sa QStyle, QStylePainter*//*! \enum QStyleOption::OptionType This enum is used internally by QStyleOption, its subclasses, and qstyleoption_cast() to determine the type of style option. In general you do not need to worry about this unless you want to create your own QStyleOption subclass and your own styles. \value SO_Button \l QStyleOptionButton \value SO_ComboBox \l QStyleOptionComboBox \value SO_Complex \l QStyleOptionComplex \value SO_Default QStyleOption \value SO_DockWidget \l QStyleOptionDockWidget \value SO_FocusRect \l QStyleOptionFocusRect \value SO_Frame \l QStyleOptionFrame \l QStyleOptionFrameV2 \value SO_GraphicsItem \l QStyleOptionGraphicsItem \value SO_GroupBox \l QStyleOptionGroupBox \value SO_Header \l QStyleOptionHeader \value SO_MenuItem \l QStyleOptionMenuItem \value SO_ProgressBar \l QStyleOptionProgressBar \l QStyleOptionProgressBarV2 \value SO_RubberBand \l QStyleOptionRubberBand \value SO_SizeGrip \l QStyleOptionSizeGrip \value SO_Slider \l QStyleOptionSlider \value SO_SpinBox \l QStyleOptionSpinBox \value SO_Tab \l QStyleOptionTab \value SO_TabBarBase \l QStyleOptionTabBarBase \value SO_TabWidgetFrame \l QStyleOptionTabWidgetFrame \value SO_TitleBar \l QStyleOptionTitleBar \value SO_ToolBar \l QStyleOptionToolBar \value SO_ToolBox \l QStyleOptionToolBox \value SO_ToolButton \l QStyleOptionToolButton \value SO_ViewItem \l QStyleOptionViewItem (used in Interviews) The following values are used for custom controls: \value SO_CustomBase Reserved for custom QStyleOptions; all custom controls values must be above this value \value SO_ComplexCustomBase Reserved for custom QStyleOptions; all custom complex controls values must be above this value Some style options are defined for various Qt3Support controls: \value SO_Q3DockWindow \l QStyleOptionQ3DockWindow \value SO_Q3ListView \l QStyleOptionQ3ListView \value SO_Q3ListViewItem \l QStyleOptionQ3ListViewItem \sa type*//*! Constructs a QStyleOption with the specified \a version and \a type. The version has no special meaning for QStyleOption; it can be used by subclasses to distinguish between different version of the same option type. The \l state member variable is initialized to QStyle::State_None. \sa version, type*/QStyleOption::QStyleOption(int version, int type) : version(version), type(type), state(QStyle::State_None), direction(QApplication::layoutDirection()), fontMetrics(QFont()){}/*! Destroys this style option object.*/QStyleOption::~QStyleOption(){}/*! \fn void QStyleOption::initFrom(const QWidget *widget) \since 4.1 Initializes the \l state, \l direction, \l rect, \l palette, and \l fontMetrics member variables based on the specified \a widget. This is a convenience function; the member variables can also be initialized manually. \sa QWidget::layoutDirection(), QWidget::rect(), QWidget::palette(), QWidget::fontMetrics()*//*! \obsolete Use initFrom(\a widget) instead.*/void QStyleOption::init(const QWidget *widget){ QWidget *window = widget->window(); state = QStyle::State_None; if (widget->isEnabled()) state |= QStyle::State_Enabled; if (widget->hasFocus()) state |= QStyle::State_HasFocus; if (window->testAttribute(Qt::WA_KeyboardFocusChange)) state |= QStyle::State_KeyboardFocusChange; if (widget->underMouse()) state |= QStyle::State_MouseOver; if (window->isActiveWindow()) state |= QStyle::State_Active; if (widget->isWindow()) state |= QStyle::State_Window;#ifdef Q_WS_MAC extern bool qt_mac_can_clickThrough(const QWidget *w); //qwidget_mac.cpp if (!(state & QStyle::State_Active) && !qt_mac_can_clickThrough(widget)) state &= ~QStyle::State_Enabled; switch (QMacStyle::widgetSizePolicy(widget)) { case QMacStyle::SizeSmall: state |= QStyle::State_Small; break; case QMacStyle::SizeMini: state |= QStyle::State_Mini; break; default: ; }#endif#ifdef QT_KEYPAD_NAVIGATION if (widget->hasEditFocus()) state |= QStyle::State_HasEditFocus;#endif direction = widget->layoutDirection(); rect = widget->rect(); palette = widget->palette(); fontMetrics = widget->fontMetrics();}/*! Constructs a copy of \a other.*/QStyleOption::QStyleOption(const QStyleOption &other) : version(Version), type(Type), state(other.state), direction(other.direction), rect(other.rect), fontMetrics(other.fontMetrics), palette(other.palette){}/*! Assign \a other to this QStyleOption.*/QStyleOption &QStyleOption::operator=(const QStyleOption &other){ state = other.state; direction = other.direction; rect = other.rect; fontMetrics = other.fontMetrics; palette = other.palette; return *this;}/*! \enum QStyleOption::StyleOptionType This enum is used to hold information about the type of the style option, and is defined for each QStyleOption subclass. \value Type The type of style option provided (\l{SO_Default} for this class). The type is used internally by QStyleOption, its subclasses, and qstyleoption_cast() to determine the type of style option. In general you do not need to worry about this unless you want to create your own QStyleOption subclass and your own styles. \sa StyleOptionVersion*//*! \enum QStyleOption::StyleOptionVersion This enum is used to hold information about the version of the style option, and is defined for each QStyleOption subclass. \value Version 1 The version is used by QStyleOption subclasses to implement extensions without breaking compatibility. If you use qstyleoption_cast(), you normally don't need to check it. \sa StyleOptionType*//*! \variable QStyleOption::palette \brief the palette that should be used when painting the control By default, the application's default palette is used. \sa initFrom()*//*! \variable QStyleOption::direction \brief the text layout direction that should be used when drawing text in the control By default, the layout direction is Qt::LeftToRight. \sa initFrom()*//*! \variable QStyleOption::fontMetrics \brief the font metrics that should be used when drawing text in the control By default, the application's default font is used. \sa initFrom()*//*! \variable QStyleOption::rect \brief the area that should be used for various calculations and painting This can have different meanings for different types of elements. For example, for a \l QStyle::CE_PushButton element it would be the rectangle for the entire button, while for a \l QStyle::CE_PushButtonLabel element it would be just the area for the push button label. The default value is a null rectangle, i.e. a rectangle with both the width and the height set to 0. \sa initFrom()*//*! \variable QStyleOption::state \brief the style flags that are used when drawing the control The default value is QStyle::State_None. \sa initFrom(), QStyle::drawPrimitive(), QStyle::drawControl(), QStyle::drawComplexControl(), QStyle::State*//*! \variable QStyleOption::type \brief the option type of the style option The default value is SO_Default. \sa OptionType*//*! \variable QStyleOption::version \brief the version of the style option This value can be used by subclasses to implement extensions without breaking compatibility. If you use the qstyleoption_cast() function, you normally don't need to check it. The default value is 1.*//*! \class QStyleOptionFocusRect \brief The QStyleOptionFocusRect class is used to describe the parameters for drawing a focus rectangle with QStyle. For performance reasons, the access to the member variables is direct (i.e., using the \c . or \c -> operator). This low-level feel makes the structures straightforward to use and emphasizes that these are simply parameters used by the style functions. For an example demonstrating how style options can be used, see the \l {widgets/styles}{Styles} example. \sa QStyleOption*//*! Constructs a QStyleOptionFocusRect, initializing the members variables to their default values.*/QStyleOptionFocusRect::QStyleOptionFocusRect() : QStyleOption(Version, SO_FocusRect){ state |= QStyle::State_KeyboardFocusChange; // assume we had one, will be corrected in initFrom()}/*! \internal*/QStyleOptionFocusRect::QStyleOptionFocusRect(int version) : QStyleOption(version, SO_FocusRect){ state |= QStyle::State_KeyboardFocusChange; // assume we had one, will be corrected in initFrom()}/*! \enum QStyleOptionFocusRect::StyleOptionType This enum is used to hold information about the type of the style option, and is defined for each QStyleOption subclass. \value Type The type of style option provided (\l{SO_FocusRect} for this class). The type is used internally by QStyleOption, its subclasses, and qstyleoption_cast() to determine the type of style option. In general you do not need to worry about this unless you want to create your own QStyleOption subclass and your own styles. \sa StyleOptionVersion*//*! \enum QStyleOptionFocusRect::StyleOptionVersion This enum is used to hold information about the version of the style option, and is defined for each QStyleOption subclass. \value Version 1 The version is used by QStyleOption subclasses to implement extensions without breaking compatibility. If you use qstyleoption_cast(), you normally don't need to check it. \sa StyleOptionType*//*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -