QStyleOption 类

QStyleOption class stores the parameters used by QStyle 函数。 更多...

头: #include <QStyleOption>
qmake: QT += widgets
继承者:

QStyleOptionButton , QStyleOptionComplex , QStyleOptionDockWidget , QStyleOptionFocusRect , QStyleOptionFrame , QStyleOptionGraphicsItem , QStyleOptionHeader , QStyleOptionMenuItem , QStyleOptionProgressBar , QStyleOptionRubberBand , QStyleOptionTab , QStyleOptionTabBarBase , QStyleOptionTabWidgetFrame , QStyleOptionToolBar , QStyleOptionToolBox ,和 QStyleOptionViewItem

公共类型

enum OptionType { SO_Button, SO_ComboBox, SO_Complex, SO_Default, ..., SO_ComplexCustomBase }
enum StyleOptionType { Type }
enum StyleOptionVersion { Version }

公共函数

QStyleOption (int version = QStyleOption::Version, int type = SO_Default)
QStyleOption (const QStyleOption & other )
~QStyleOption ()
void initFrom (const QWidget * widget )
QStyleOption & operator= (const QStyleOption & other )

公共变量

Qt::LayoutDirection direction
QFontMetrics fontMetrics
QPalette palette
QRect rect
QStyle::State state
QObject * styleObject
int type
int version
T qstyleoption_cast (const QStyleOption * option )
T qstyleoption_cast (QStyleOption * option )

详细描述

QStyleOption class stores the parameters used by QStyle 函数。

QStyleOption and its subclasses contain all the information that QStyle 函数需要绘制图形元素。

出于性能原因,有很少成员函数,且对成员变量的访问是直接的 (即:使用 . or -> 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 隐式共享 for types such as QString , QPalette ,和 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:

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);
}
					

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:

void MyStyle::drawPrimitive(PrimitiveElement element,
                            const QStyleOption *option,
                            QPainter *painter,
                            const QWidget *widget)
{
    if (element == PE_FrameFocusRect) {
        const QStyleOptionFocusRect *focusRectOption =
                qstyleoption_cast<const QStyleOptionFocusRect *>(option);
        if (focusRectOption) {
            // ...
        }
    }
    // ...
}
					

qstyleoption_cast () function will return 0 if the object to which option points is not of the correct type.

对于可以如何使用样式选项的演示范例,见 样式 范例。

另请参阅 QStyle and QStylePainter .

成员类型文档编制

enum QStyleOption:: OptionType

此枚举被内部使用,通过 QStyleOption ,其子类,和 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.

常量 描述
QStyleOption::SO_Button 2 QStyleOptionButton
QStyleOption::SO_ComboBox ? QStyleOptionComboBox
QStyleOption::SO_Complex 0xf0000 QStyleOptionComplex
QStyleOption::SO_Default 0 QStyleOption
QStyleOption::SO_DockWidget 9 QStyleOptionDockWidget
QStyleOption::SO_FocusRect 1 QStyleOptionFocusRect
QStyleOption::SO_Frame 5 QStyleOptionFrame
QStyleOption::SO_GraphicsItem 15 QStyleOptionGraphicsItem
QStyleOption::SO_GroupBox ? QStyleOptionGroupBox
QStyleOption::SO_Header 8 QStyleOptionHeader
QStyleOption::SO_MenuItem 4 QStyleOptionMenuItem
QStyleOption::SO_ProgressBar 6 QStyleOptionProgressBar
QStyleOption::SO_RubberBand 13 QStyleOptionRubberBand
QStyleOption::SO_SizeGrip ? QStyleOptionSizeGrip
QStyleOption::SO_Slider ? QStyleOptionSlider
QStyleOption::SO_SpinBox ? QStyleOptionSpinBox
QStyleOption::SO_Tab 3 QStyleOptionTab
QStyleOption::SO_TabBarBase 12 QStyleOptionTabBarBase
QStyleOption::SO_TabWidgetFrame 11 QStyleOptionTabWidgetFrame
QStyleOption::SO_TitleBar ? QStyleOptionTitleBar
QStyleOption::SO_ToolBar 14 QStyleOptionToolBar
QStyleOption::SO_ToolBox 7 QStyleOptionToolBox
QStyleOption::SO_ToolButton ? QStyleOptionToolButton
QStyleOption::SO_ViewItem 10 QStyleOptionViewItem (used in Interviews)

The following values are used for custom controls:

常量 描述
QStyleOption::SO_CustomBase 0xf00 Reserved for custom QStyleOptions; all custom controls values must be above this value
QStyleOption::SO_ComplexCustomBase 0xf000000 Reserved for custom QStyleOptions; all custom complex controls values must be above this value

另请参阅 type .

enum QStyleOption:: StyleOptionType

This enum is used to hold information about the type of the style option, and is defined for each QStyleOption 子类。

常量 描述
QStyleOption::Type SO_Default 提供样式选项的类型 ( SO_Default 对于此类)。

类型内部使用通过 QStyleOption ,其子类,和 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.

另请参阅 StyleOptionVersion .

enum QStyleOption:: StyleOptionVersion

This enum is used to hold information about the version of the style option, and is defined for each QStyleOption 子类。

常量 描述
QStyleOption::Version 1 1

版本的使用通过 QStyleOption 子类以实现扩展在不破坏兼容性的情况下。若使用 qstyleoption_cast (), you normally do not need to check it.

另请参阅 StyleOptionType .

成员函数文档编制

QStyleOption:: QStyleOption ( int version = QStyleOption::Version, int type = SO_Default)

构造 QStyleOption 采用指定 version and 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.

state 成员变量被初始化为 QStyle::State_None .

另请参阅 version and type .

QStyleOption:: QStyleOption (const QStyleOption & other )

构造副本为 other .

QStyleOption:: ~QStyleOption ()

销毁此样式选项对象。

void QStyleOption:: initFrom (const QWidget * widget )

初始化 state , direction , rect , palette , fontMetrics and styleObject 成员变量基于指定的 widget .

This is a convenience function; the member variables can also be initialized manually.

该函数在 Qt 4.1 引入。

另请参阅 QWidget::layoutDirection (), QWidget::rect (), QWidget::palette (),和 QWidget::fontMetrics ().

QStyleOption &QStyleOption:: operator= (const QStyleOption & other )

Assign other 到此 QStyleOption .

成员变量文档编制

Qt::LayoutDirection QStyleOption:: direction

此变量保持应使用的文本布局方向,当在控件中绘制文本时

默认情况下,布局方向为 Qt::LeftToRight .

另请参阅 initFrom ().

QFontMetrics QStyleOption:: fontMetrics

This variable holds the font metrics that should be used when drawing text in the control

By default, the application's default font is used.

另请参阅 initFrom ().

QPalette QStyleOption:: palette

This variable holds the palette that should be used when painting the control

By default, the application's default palette is used.

另请参阅 initFrom ().

QRect QStyleOption:: rect

This variable holds 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 QStyle::CE_PushButton element it would be the rectangle for the entire button, while for a 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.

另请参阅 initFrom ().

QStyle::State QStyleOption:: state

This variable holds the style flags that are used when drawing the control

默认值为 QStyle::State_None .

另请参阅 initFrom (), QStyle::drawPrimitive (), QStyle::drawControl (), QStyle::drawComplexControl (),和 QStyle::State .

QObject *QStyleOption:: styleObject

This variable holds the object being styled

The built-in styles support the following types: QWidget , QGraphicsObject and QQuickItem .

另请参阅 initFrom ().

int QStyleOption:: type

This variable holds the option type of the style option

默认值为 SO_Default .

另请参阅 OptionType .

int QStyleOption:: version

此变量保存样式选项的版本

This value can be used by subclasses to implement extensions without breaking compatibility. If you use the qstyleoption_cast () function, you normally do not need to check it.

默认值为 1。

相关非成员

T qstyleoption_cast (const QStyleOption * option )

Returns a T or 0 depending on the type and version 为给定 option .

范例:

void MyStyle::drawPrimitive(PrimitiveElement element,
                            const QStyleOption *option,
                            QPainter *painter,
                            const QWidget *widget)
{
    if (element == PE_FrameFocusRect) {
        const QStyleOptionFocusRect *focusRectOption =
                qstyleoption_cast<const QStyleOptionFocusRect *>(option);
        if (focusRectOption) {
            // ...
        }
    }
    // ...
}
					

另请参阅 QStyleOption::type and QStyleOption::version .

T qstyleoption_cast ( QStyleOption * option )

这是重载函数。

Returns a T or 0 depending on the type of the given option .