The 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 ) |
The 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.
調用者的 QStyle function usually creates QStyleOption objects on the stack. This combined with Qt's extensive use of 隱式共享 對於類型譬如 QString , QPalette ,和 QColor 確保不發生不必要的內存分配。
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); }
範例中,控件是 QStyle::CE_PushButton ,和根據 QStyle::drawControl () 文檔編製,相應類是 QStyleOptionButton .
當重實現 QStyle functions that take a QStyleOption parameter, you often need to cast the QStyleOption to a subclass. For safety, you can use qstyleoption_cast () 以確保指針類型正確。例如:
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) { // ... } } // ... }
The
qstyleoption_cast
() 函數將返迴 0 若對象指嚮的
option
類型不正確。
對於可以如何使用樣式選項的演示範例,見 樣式 範例。
另請參閱 QStyle and QStylePainter .
此枚舉被內部使用,通過 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
|
0xf0004
|
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
|
0xf0006
|
QStyleOptionGroupBox |
QStyleOption::SO_Header
|
8
|
QStyleOptionHeader |
QStyleOption::SO_MenuItem
|
4
|
QStyleOptionMenuItem |
QStyleOption::SO_ProgressBar
|
6
|
QStyleOptionProgressBar |
QStyleOption::SO_RubberBand
|
13
|
QStyleOptionRubberBand |
QStyleOption::SO_SizeGrip
|
0xf0007
|
QStyleOptionSizeGrip |
QStyleOption::SO_Slider
|
0xf0001
|
QStyleOptionSlider |
QStyleOption::SO_SpinBox
|
0xf0002
|
QStyleOptionSpinBox |
QStyleOption::SO_Tab
|
3
|
QStyleOptionTab |
QStyleOption::SO_TabBarBase
|
12
|
QStyleOptionTabBarBase |
QStyleOption::SO_TabWidgetFrame
|
11
|
QStyleOptionTabWidgetFrame |
QStyleOption::SO_TitleBar
|
0xf0005
|
QStyleOptionTitleBar |
QStyleOption::SO_ToolBar
|
14
|
QStyleOptionToolBar |
QStyleOption::SO_ToolBox
|
7
|
QStyleOptionToolBox |
QStyleOption::SO_ToolButton
|
0xf0003
|
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 .
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 .
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 (),通常不需要校驗它。
另請參閱 StyleOptionType .
構造 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.
The state 成員變量被初始化為 QStyle::State_None .
構造副本為 other .
銷毀此樣式選項對象。
初始化 state , direction , rect , palette , fontMetrics and styleObject 成員變量基於指定的 widget .
這是方便函數;還可以手動初始化成員變量。
該函數在 Qt 4.1 引入。
另請參閱 QWidget::layoutDirection (), QWidget::rect (), QWidget::palette (),和 QWidget::fontMetrics ().
賦值 other 到此 QStyleOption .
此變量保持應使用的文本布局方嚮,當在控件中繪製文本時
默認情況下,布局方嚮為 Qt::LeftToRight .
另請參閱 initFrom ().
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 ().
This variable holds the palette that should be used when painting the control
默認情況下,使用應用程序的默認調色闆。
另請參閱 initFrom ().
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_None .
另請參閱 initFrom (), QStyle::drawPrimitive (), QStyle::drawControl (), QStyle::drawComplexControl (),和 QStyle::State .
此變量保持要樣式化的對象
內置樣式支持下列類型: QWidget , QGraphicsObject and QQuickItem .
另請參閱 initFrom ().
此變量保持樣式選項的選項類型
默認值為 SO_Default .
另請參閱 OptionType .
此變量保存樣式選項的版本
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。
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 .
這是重載函數。
Returns a T or 0 depending on the type of the given option .