📄 qstyleoption.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 "qstyleoption.h"#include "qapplication.h"#ifdef Q_WS_MAC# include "private/qt_mac_p.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: \code void MyPushButton::paintEvent(QPaintEvent *) { QStyleOptionButton option; option.initFrom(this); option.state = isDown() ? QStyle::State_Sunken : QStyle::State_Raised; if (isDefault()) option.features |= QStyleOptionButton::DefaultButton; option.text = text(); option.icon = icon(); QPainter painter(this); style().drawControl(QStyle::CE_PushButton, &option, &painter, this); } \endcode 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<T>() to ensure that the pointer type is correct. For example: \code void MyStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) { if (element == PE_FocusRect) { const QStyleOptionFocusRect *focusRectOption = qstyleoption_cast<const QStyleOptionFocusRect *>(option); if (focusRectOption) { ... } } else { ... } } \endcode qstyleoption_cast<T>() will return 0 if the object to which \c option points isn't of the correct type. \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_Default QStyleOption \value SO_FocusRect \l QStyleOptionFocusRect \value SO_Button \l QStyleOptionButton \value SO_Tab \l QStyleOptionTab \value SO_TabWidgetFrame \l QStyleOptionTabWidgetFrame \value SO_TabBarBase \l QStyleOptionTabBarBase \value SO_MenuItem \l QStyleOptionMenuItem \value SO_Complex \l QStyleOptionComplex \value SO_Slider \l QStyleOptionSlider \value SO_Frame \l QStyleOptionFrame \l QStyleOptionFrameV2 \value SO_GroupBox \l QStyleOptionGroupBox \value SO_ProgressBar \l QStyleOptionProgressBar \l QStyleOptionProgressBarV2 \value SO_Q3ListView \l QStyleOptionQ3ListView \value SO_Q3ListViewItem \l QStyleOptionQ3ListViewItem \value SO_Header \l QStyleOptionHeader \value SO_Q3DockWindow \l QStyleOptionQ3DockWindow \value SO_DockWidget \l QStyleOptionDockWidget \value SO_SpinBox \l QStyleOptionSpinBox \value SO_ToolButton \l QStyleOptionToolButton \value SO_ComboBox \l QStyleOptionComboBox \value SO_ToolBox \l QStyleOptionToolBox \value SO_ToolBar \l QStyleOptionToolBar \value SO_RubberBand \l QStyleOptionRubberBand \value SO_TitleBar \l QStyleOptionTitleBar \value SO_ViewItem \l QStyleOptionViewItem (used in Interviews) \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*//*! Constructs a QStyleOption with version \a version and type \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 the 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 \a widget. This function is provided only for convenience. You can also initialize the variables manually if you want. \sa QWidget::layoutDirection(), QWidget::rect(), QWidget::palette(), QWidget::fontMetrics()*//*! \obsolete Use initFrom(\a widget) instead.*/void QStyleOption::init(const QWidget *widget){ state = QStyle::State_None; if (widget->isEnabled()) state |= QStyle::State_Enabled; if (widget->hasFocus()) state |= QStyle::State_HasFocus; if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange)) state |= QStyle::State_KeyboardFocusChange; if (widget->underMouse()) state |= QStyle::State_MouseOver; if (widget->window()->isActiveWindow()) state |= QStyle::State_Active;#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;}/*! \variable QStyleOption::Type Equals SO_Default.*//*! \variable QStyleOption::Version Equals 1.*//*! \variable QStyleOption::palette \brief the palette that should be used when painting the control*//*! \variable QStyleOption::direction \brief the text layout direction that should be used when drawing text in the control*//*! \variable QStyleOption::fontMetrics \brief the font metrics that should be used when drawing text in the control*//*! \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 \l QStyle::CE_PushButton it would be the rectangle for the entire button, while for \l QStyle::CE_PushButtonLabel it would be just the area for the push button label.*//*! \variable QStyleOption::state \brief the style flags that are used when drawing the control \sa QStyle::drawPrimitive(), QStyle::drawControl(), QStyle::drawComplexControl(), QStyle::State*//*! \variable QStyleOption::type \brief the option type of the style option \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 qstyleoption_cast<T>(), you normally don't need to check it.*//*! \class QStyleOptionFocusRect \brief The QStyleOptionFocusRect class is used to describe the parameters for drawing a focus rectangle with QStyle.*//*! Constructs a QStyleOptionFocusRect. The members variables are initialized to 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()}/*! \variable QStyleOptionFocusRect::Type Equals SO_FocusRect.*//*! \variable QStyleOptionFocusRect::Version Equals 1.*//*! \fn QStyleOptionFocusRect::QStyleOptionFocusRect(const QStyleOptionFocusRect &other) Constructs a copy of the \a other style option.*//*! \variable QStyleOptionFocusRect::backgroundColor \brief The background color on which the focus rectangle is being drawn.*//*! \class QStyleOptionFrame \brief The QStyleOptionFrame class is used to describe the parameters for drawing a frame. QStyleOptionFrame is used for drawing several built-in Qt widget, including QFrame, QGroupBox, QLineEdit, and QMenu. Note that to describe the parameters necessary for drawing a frame in Qt 4.1 or above, you must use the QStyleOptionFrameV2 subclass. An instance of the QStyleOptionFrame class has \l type SO_Frame and \l version 1. 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. 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. If you create your own QStyle subclass, you should handle both QStyleOptionFrame and QStyleOptionFrameV2. For an example demonstrating how style options can be used, see the \l {widgets/styles}{Styles} example. \sa QStyleOptionFrameV2, QStyleOption*//*! Constructs a QStyleOptionFrame. The members variables are initialized to default values.*/QStyleOptionFrame::QStyleOptionFrame() : QStyleOption(Version, SO_Frame), lineWidth(0), midLineWidth(0){}/*! \internal*/QStyleOptionFrame::QStyleOptionFrame(int version) : QStyleOption(version, SO_Frame), lineWidth(0), midLineWidth(0){}/*! \fn QStyleOptionFrame::QStyleOptionFrame(const QStyleOptionFrame &other) Constructs a copy of the \a other style option.*//*! \variable QStyleOptionFrame::Type Equals SO_Frame. 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.*//*! \variable QStyleOptionFrame::Version Equals 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.*//*! \variable QStyleOptionFrame::lineWidth \brief The line width for drawing the frame. \sa QFrame::lineWidth*//*! \variable QStyleOptionFrame::midLineWidth \brief The mid-line width for drawing the frame. This is usually used in drawing sunken or raised frames. \sa QFrame::midLineWidth*//*! \class QStyleOptionFrameV2 \brief The QStyleOptionFrameV2 class is used to describe the parameters necessary for drawing a frame in Qt 4.1 or above. \since 4.1 QStyleOptionFrameV2 inherits QStyleOptionFrame which is used for drawing several built-in Qt widget, including QFrame, QGroupBox, QLineEdit, and QMenu. An instance of the QStyleOptionFrameV2 class has \l type SO_Frame and \l version 2. 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. 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. If you create your own QStyle subclass, you should handle both QStyleOptionFrame and QStyleOptionFrameV2. One way to achieve this is to use the QStyleOptionFrameV2 copy constructor. For example: \code if (const QStyleOptionFrame *frameOption = qstyleoption_cast<const QStyleOptionFrame *>(option)) { QStyleOptionFrameV2 frameOptionV2(*frameOption); // draw the frame using frameOptionV2 } \endcode In the example above: If the \c frameOption's version is 1, \l FrameFeature is set to \l None for \c frameOptionV2. If \c frameOption's version is 2, the constructor will simply copy the \c frameOption's \l FrameFeature value.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -