
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQStyleOption class stores the parameters used byQStyle functions.More...
| enum | OptionType { SO_Button, SO_ComboBox, SO_Complex, SO_Default, ..., SO_Q3ListViewItem } |
| 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 |
| int | type |
| int | version |
| T | qstyleoption_cast(const QStyleOption * option) |
| T | qstyleoption_cast(QStyleOption * option) |
TheQStyleOption class stores the parameters used byQStyle functions.
QStyleOption and its subclasses contain all the information thatQStyle 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. 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 aQStyle function usually createsQStyleOption objects on the stack. This combined with Qt's extensive use ofimplicit sharing for types such asQString,QPalette, andQColor ensures that no memory allocation needlessly takes place.
The following code snippet shows how to use a specificQStyleOption 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 aQStyle::CE_PushButton, and according to theQStyle::drawControl() documentation the corresponding class isQStyleOptionButton.
When reimplementingQStyle functions that take aQStyleOption parameter, you often need to cast theQStyleOption to a subclass. For safety, you can useqstyleoption_cast() to ensure that the pointer type is correct. For example:
void MyStyle::drawPrimitive(PrimitiveElement element,constQStyleOption*option,QPainter*painter,constQWidget*widget){if (element== PE_FrameFocusRect) {constQStyleOptionFocusRect*focusRectOption= qstyleoption_cast<constQStyleOptionFocusRect*>(option);if (focusRectOption) {// ... } }// ...}
Theqstyleoption_cast() function will return 0 if the object to whichoption points is not of the correct type.
For an example demonstrating how style options can be used, see theStyles example.
See alsoQStyle andQStylePainter.
This enum is used internally byQStyleOption, its subclasses, andqstyleoption_cast() to determine the type of style option. In general you do not need to worry about this unless you want to create your ownQStyleOption subclass and your own styles.
| Constant | Value | Description |
|---|---|---|
QStyleOption::SO_Button | 2 | QStyleOptionButton |
QStyleOption::SO_ComboBox | ? | QStyleOptionComboBox |
QStyleOption::SO_Complex | 0xf0000 | QStyleOptionComplex |
QStyleOption::SO_Default | 0 | QStyleOption |
QStyleOption::SO_DockWidget | 10 | QStyleOptionDockWidget |
QStyleOption::SO_FocusRect | 1 | QStyleOptionFocusRect |
QStyleOption::SO_Frame | 5 | QStyleOptionFrameQStyleOptionFrameV2 |
QStyleOption::SO_GraphicsItem | 17 | QStyleOptionGraphicsItem |
QStyleOption::SO_GroupBox | ? | QStyleOptionGroupBox |
QStyleOption::SO_Header | 8 | QStyleOptionHeader |
QStyleOption::SO_MenuItem | 4 | QStyleOptionMenuItem |
QStyleOption::SO_ProgressBar | 6 | QStyleOptionProgressBarQStyleOptionProgressBarV2 |
QStyleOption::SO_RubberBand | 15 | QStyleOptionRubberBand |
QStyleOption::SO_SizeGrip | ? | QStyleOptionSizeGrip |
QStyleOption::SO_Slider | ? | QStyleOptionSlider |
QStyleOption::SO_SpinBox | ? | QStyleOptionSpinBox |
QStyleOption::SO_Tab | 3 | QStyleOptionTab |
QStyleOption::SO_TabBarBase | 14 | QStyleOptionTabBarBase |
QStyleOption::SO_TabWidgetFrame | 13 | QStyleOptionTabWidgetFrame |
QStyleOption::SO_TitleBar | ? | QStyleOptionTitleBar |
QStyleOption::SO_ToolBar | 16 | QStyleOptionToolBar |
QStyleOption::SO_ToolBox | 7 | QStyleOptionToolBox |
QStyleOption::SO_ToolButton | ? | QStyleOptionToolButton |
QStyleOption::SO_ViewItem | 12 | QStyleOptionViewItem (used in Interviews) |
The following values are used for custom controls:
| Constant | Value | Description |
|---|---|---|
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 |
Some style options are defined for variousQt3Support controls:
| Constant | Value | Description |
|---|---|---|
QStyleOption::SO_Q3DockWindow | 9 | QStyleOptionQ3DockWindow |
QStyleOption::SO_Q3ListView | ? | QStyleOptionQ3ListView |
QStyleOption::SO_Q3ListViewItem | 11 | QStyleOptionQ3ListViewItem |
See alsotype.
This enum is used to hold information about the type of the style option, and is defined for eachQStyleOption subclass.
| Constant | Value | Description |
|---|---|---|
QStyleOption::Type | SO_Default | The type of style option provided (SO_Default for this class). |
The type is used internally byQStyleOption, its subclasses, andqstyleoption_cast() to determine the type of style option. In general you do not need to worry about this unless you want to create your ownQStyleOption subclass and your own styles.
See alsoStyleOptionVersion.
This enum is used to hold information about the version of the style option, and is defined for eachQStyleOption subclass.
| Constant | Value | Description |
|---|---|---|
QStyleOption::Version | 1 | 1 |
The version is used byQStyleOption subclasses to implement extensions without breaking compatibility. If you useqstyleoption_cast(), you normally do not need to check it.
See alsoStyleOptionType.
Constructs aQStyleOption with the specifiedversion andtype.
The version has no special meaning forQStyleOption; it can be used by subclasses to distinguish between different version of the same option type.
Thestate member variable is initialized toQStyle::State_None.
Constructs a copy ofother.
Destroys this style option object.
Initializes thestate,direction,rect,palette, andfontMetrics member variables based on the specifiedwidget.
This is a convenience function; the member variables can also be initialized manually.
This function was introduced in Qt 4.1.
See alsoQWidget::layoutDirection(),QWidget::rect(),QWidget::palette(), andQWidget::fontMetrics().
Assignother to thisQStyleOption.
This variable holds the text layout direction that should be used when drawing text in the control.
By default, the layout direction isQt::LeftToRight.
See alsoinitFrom().
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.
See alsoinitFrom().
This variable holds the palette that should be used when painting the control.
By default, the application's default palette is used.
See alsoinitFrom().
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 aQStyle::CE_PushButton element it would be the rectangle for the entire button, while for aQStyle::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.
See alsoinitFrom().
This variable holds the style flags that are used when drawing the control.
The default value isQStyle::State_None.
See alsoinitFrom(),QStyle::drawPrimitive(),QStyle::drawControl(),QStyle::drawComplexControl(), andQStyle::State.
This variable holds the option type of the style option.
The default value isSO_Default.
See alsoOptionType.
This variable holds the version of the style option.
This value can be used by subclasses to implement extensions without breaking compatibility. If you use theqstyleoption_cast() function, you normally do not need to check it.
The default value is 1.
Returns a T or 0 depending on thetype andversion of the givenoption.
Example:
void MyStyle::drawPrimitive(PrimitiveElement element,constQStyleOption*option,QPainter*painter,constQWidget*widget){if (element== PE_FrameFocusRect) {constQStyleOptionFocusRect*focusRectOption= qstyleoption_cast<constQStyleOptionFocusRect*>(option);if (focusRectOption) {// ... } }// ...}
See alsoQStyleOption::type andQStyleOption::version.
This is an overloaded function.
Returns a T or 0 depending on the type of the givenoption.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.