QStyleOption Class▲
-
Header: QStyleOption
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
-
qmake: QT += widgets
-
Inherited By: QStyleOptionButton, QStyleOptionComplex, QStyleOptionDockWidget, QStyleOptionFocusRect, QStyleOptionFrame, QStyleOptionGraphicsItem, QStyleOptionHeader, QStyleOptionMenuItem, QStyleOptionProgressBar, QStyleOptionRubberBand, QStyleOptionTab, QStyleOptionTabBarBase, QStyleOptionTabWidgetFrame, QStyleOptionToolBar, QStyleOptionToolBox, and QStyleOptionViewItem
-
Group: QStyleOption is part of appearance
Detailed Description▲
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 . or -> operator). This 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 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:
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()-&
gt;drawControl(QStyle::
CE_PushButton, &
amp;option, &
amp;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&
lt;const
QStyleOptionFocusRect *&
gt;(option);
if
(focusRectOption) {
// ...
}
}
// ...
}
The qstyleoption_cast() function will return 0 if the object to which option points is not of the correct type.
For an example demonstrating how style options can be used, see the Styles example.
See Also▲
See also QStyle, QStylePainter
Member Type Documentation▲
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.
Constant |
Value |
Description |
---|---|---|
QStyleOption::SO_Button |
2 |
|
QStyleOption::SO_ComboBox |
0xf0004 |
|
QStyleOption::SO_Complex |
0xf0000 |
|
QStyleOption::SO_Default |
0 |
|
QStyleOption::SO_DockWidget |
9 |
|
QStyleOption::SO_FocusRect |
1 |
|
QStyleOption::SO_Frame |
5 |
|
QStyleOption::SO_GraphicsItem |
15 |
|
QStyleOption::SO_GroupBox |
0xf0006 |
|
QStyleOption::SO_Header |
8 |
|
QStyleOption::SO_MenuItem |
4 |
|
QStyleOption::SO_ProgressBar |
6 |
|
QStyleOption::SO_RubberBand |
13 |
|
QStyleOption::SO_SizeGrip |
0xf0007 |
|
QStyleOption::SO_Slider |
0xf0001 |
|
QStyleOption::SO_SpinBox |
0xf0002 |
|
QStyleOption::SO_Tab |
3 |
|
QStyleOption::SO_TabBarBase |
12 |
|
QStyleOption::SO_TabWidgetFrame |
11 |
|
QStyleOption::SO_TitleBar |
0xf0005 |
|
QStyleOption::SO_ToolBar |
14 |
|
QStyleOption::SO_ToolBox |
7 |
|
QStyleOption::SO_ToolButton |
0xf0003 |
|
QStyleOption::SO_ViewItem |
10 |
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 |
See Also▲
See also type
enum QStyleOption::StyleOptionType▲
This enum is used to hold information about the type of the style option, and is defined for each QStyleOption subclass.
Constant |
Value |
Description |
---|---|---|
QStyleOption::Type |
SO_Default |
The type of style option provided (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.
See Also▲
See also 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.
Constant |
Value |
Description |
---|---|---|
QStyleOption::Version |
1 |
1 |
The version is used by QStyleOption subclasses to implement extensions without breaking compatibility. If you use qstyleoption_cast(), you normally do not need to check it.
See Also▲
See also StyleOptionType
Member Function Documentation▲
QStyleOption::QStyleOption(int version = QStyleOption::Version, int type = SO_Default)▲
Constructs a QStyleOption with the specified 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 member variable is initialized to QStyle::State_None.
See Also▲
QStyleOption::QStyleOption(const QStyleOption &other)▲
Constructs a copy of other.
QStyleOption::~QStyleOption()▲
Destroys this style option object.
void QStyleOption::initFrom(const QWidget *widget)▲
Initializes the state, direction, rect, palette, fontMetrics and styleObject member variables based on the specified widget.
This is a convenience function; the member variables can also be initialized manually.
See Also▲
See also QWidget::layoutDirection(), QWidget::rect(), QWidget::palette(), QWidget::fontMetrics()
QStyleOption &QStyleOption::operator=(const QStyleOption &other)▲
Assign other to this QStyleOption.
Member Variable Documentation▲
Qt::LayoutDirection QStyleOption::direction▲
This variable holds the text layout direction that should be used when drawing text in the control
By default, the layout direction is Qt::LeftToRight.
See Also▲
See also 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.
See Also▲
See also 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.
See Also▲
See also 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.
See Also▲
See also initFrom()
QStyle::State QStyleOption::state▲
This variable holds the style flags that are used when drawing the control
The default value is QStyle::State_None.
See Also▲
See also 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.
See Also▲
See also initFrom()
int QStyleOption::type▲
This variable holds the option type of the style option
The default value is SO_Default.
See Also▲
See also OptionType
int QStyleOption::version▲
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 the qstyleoption_cast() function, you normally do not need to check it.
The default value is 1.
Related Non-Members▲
T qstyleoption_cast(const QStyleOption *option)▲
Returns a T or nullptr depending on the type and version of the given option.
Example:
void
MyStyle::
drawPrimitive(PrimitiveElement element,
const
QStyleOption *
option,
QPainter *
painter,
const
QWidget *
widget)
{
if
(element ==
PE_FrameFocusRect) {
const
QStyleOptionFocusRect *
focusRectOption =
qstyleoption_cast&
lt;const
QStyleOptionFocusRect *&
gt;(option);
if
(focusRectOption) {
// ...
}
}
// ...
}
See Also▲
See also QStyleOption::type, QStyleOption::version
T qstyleoption_cast(QStyleOption *option)▲
This is an overloaded function.
Returns a T or nullptr depending on the type of the given option.