Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QStyleOption Class Reference

The QStyleOption class stores the parameters used by QStyle functions. More...

#include <qstyleoption.h>

Inheritance diagram for QStyleOption:
Collaboration diagram for QStyleOption:

Public Types

enum  OptionType {
  SO_Default , SO_FocusRect , SO_Button , SO_Tab ,
  SO_MenuItem , SO_Frame , SO_ProgressBar , SO_ToolBox ,
  SO_Header , SO_DockWidget , SO_ViewItem , SO_TabWidgetFrame ,
  SO_TabBarBase , SO_RubberBand , SO_ToolBar , SO_GraphicsItem ,
  SO_Complex = 0xf0000 , SO_Slider , SO_SpinBox , SO_ToolButton ,
  SO_ComboBox , SO_TitleBar , SO_GroupBox , SO_SizeGrip ,
  SO_CustomBase = 0xf00 , SO_ComplexCustomBase = 0xf000000
}
 This enum is used internally by QStyleOption, its subclasses, and qstyleoption_cast() to determine the type of style option. More...
enum  StyleOptionType { Type = SO_Default }
 This enum is used to hold information about the type of the style option, and is defined for each QStyleOption subclass. More...
enum  StyleOptionVersion { Version = 1 }
 This enum is used to hold information about the version of the style option, and is defined for each QStyleOption subclass. More...

Public Member Functions

 QStyleOption (int version=QStyleOption::Version, int type=SO_Default)
 Constructs a QStyleOption with the specified version and type.
 QStyleOption (const QStyleOption &other)
 Constructs a copy of other.
 ~QStyleOption ()
 Destroys this style option object.
void initFrom (const QWidget *w)
QStyleOptionoperator= (const QStyleOption &other)
 Assign other to this QStyleOption.

Public Attributes

int version
int type
QStyle::State state
Qt::LayoutDirection direction
QRect rect
QFontMetrics fontMetrics
QPalette palette
QObjectstyleObject

(Note that these are not member symbols.)

template< typename T > T qstyleoption_cast (const QStyleOption *option)
 \variable QStyleOptionTitleBar::text
template< typename T > T qstyleoption_cast (QStyleOption *option)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Detailed Description

The QStyleOption class stores the parameters used by QStyle functions.

\inmodule QtWidgets

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 \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:

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 QWidget *widget)
{
if (element == PE_FrameFocusRect) {
const QStyleOptionFocusRect *focusRectOption =
if (focusRectOption) {
// ...
}
}
// ...
}

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

See also
QStyle, QStylePainter

Definition at line 39 of file qstyleoption.h.

Member Enumeration Documentation

◆ 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_Button \l QStyleOptionButton \value SO_ComboBox \l QStyleOptionComboBox \value SO_Complex \l QStyleOptionComplex \value SO_Default QStyleOption \value SO_DockWidget \l QStyleOptionDockWidget \value SO_FocusRect \l QStyleOptionFocusRect \value SO_Frame \l QStyleOptionFrame \value SO_GraphicsItem \l QStyleOptionGraphicsItem \value SO_GroupBox \l QStyleOptionGroupBox \value SO_Header \l QStyleOptionHeader \value SO_MenuItem \l QStyleOptionMenuItemV2 \value SO_ProgressBar \l QStyleOptionProgressBar \value SO_RubberBand \l QStyleOptionRubberBand \value SO_SizeGrip \l QStyleOptionSizeGrip \value SO_Slider \l QStyleOptionSlider \value SO_SpinBox \l QStyleOptionSpinBox \value SO_Tab \l QStyleOptionTab \value SO_TabBarBase \l QStyleOptionTabBarBase \value SO_TabWidgetFrame \l QStyleOptionTabWidgetFrame \value SO_TitleBar \l QStyleOptionTitleBar \value SO_ToolBar \l QStyleOptionToolBar \value SO_ToolBox \l QStyleOptionToolBox \value SO_ToolButton \l QStyleOptionToolButton \value SO_ViewItem \l QStyleOptionViewItem (used in Interviews)

The following values are used for custom controls:

\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

See also
type
Enumerator
SO_Default 
SO_FocusRect 
SO_Button 
SO_Tab 
SO_MenuItem 
SO_Frame 
SO_ProgressBar 
SO_ToolBox 
SO_Header 
SO_DockWidget 
SO_ViewItem 
SO_TabWidgetFrame 
SO_TabBarBase 
SO_RubberBand 
SO_ToolBar 
SO_GraphicsItem 
SO_Complex 
SO_Slider 
SO_SpinBox 
SO_ToolButton 
SO_ComboBox 
SO_TitleBar 
SO_GroupBox 
SO_SizeGrip 
SO_CustomBase 
SO_ComplexCustomBase 

Definition at line 42 of file qstyleoption.h.

◆ StyleOptionType

This enum is used to hold information about the type of the style option, and is defined for each QStyleOption subclass.

\value Type The type of style option provided (\l{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
StyleOptionVersion
Enumerator
Type 

Definition at line 55 of file qstyleoption.h.

◆ StyleOptionVersion

This enum is used to hold information about the version of the style option, and is defined for each QStyleOption subclass.

\value Version 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
StyleOptionType
Enumerator
Version 

Definition at line 56 of file qstyleoption.h.

Constructor & Destructor Documentation

◆ QStyleOption() [1/2]

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 \l state member variable is initialized to QStyle::State_None.

See also
version, type

Definition at line 114 of file qstyleoption.cpp.

◆ QStyleOption() [2/2]

QStyleOption::QStyleOption ( const QStyleOption & other)

Constructs a copy of other.

Definition at line 182 of file qstyleoption.cpp.

◆ ~QStyleOption()

QStyleOption::~QStyleOption ( )

Destroys this style option object.

Definition at line 124 of file qstyleoption.cpp.

Member Function Documentation

◆ initFrom()

void QStyleOption::initFrom ( const QWidget * widget)
Since
4.1

Initializes the \l state, \l direction, \l rect, \l palette, \l fontMetrics and \l styleObject member variables based on the specified widget.

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

See also
QWidget::layoutDirection(), QWidget::rect(), QWidget::palette(), QWidget::fontMetrics()

Definition at line 141 of file qstyleoption.cpp.

◆ operator=()

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

Assign other to this QStyleOption.

Definition at line 192 of file qstyleoption.cpp.

◆ qstyleoption_cast() [1/2]

template< typename T > T qstyleoption_cast< T > ( const QStyleOption * option)
related

\variable QStyleOptionTitleBar::text

the text of the title bar

The default value is an empty string.

\variable QStyleOptionTitleBar::icon

the icon for the title bar

The default value is an empty icon, i.e. an icon with neither a pixmap nor a filename.

\variable QStyleOptionTitleBar::titleBarState

the state of the title bar

This is basically the window state of the underlying widget. The default value is 0.

See also
QWidget::windowState()

\variable QStyleOptionTitleBar::titleBarFlags

the widget flags for the title bar

The default value is Qt::Widget.

See also
Qt::WindowFlags

Returns a T or \nullptr depending on the \l{QStyleOption::type}{type} and \l{QStyleOption::version}{version} of the given option.

Example:

void MyStyle::drawPrimitive(PrimitiveElement element,
const QWidget *widget)
{
if (element == PE_FrameFocusRect) {
const QStyleOptionFocusRect *focusRectOption =
if (focusRectOption) {
// ...
}
}
// ...
}
See also
QStyleOption::type, QStyleOption::version

Definition at line 685 of file qstyleoption.h.

◆ qstyleoption_cast() [2/2]

template< typename T > T qstyleoption_cast< T > ( QStyleOption * option)
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

Definition at line 697 of file qstyleoption.h.

Member Data Documentation

◆ direction

Qt::LayoutDirection QStyleOption::direction

Definition at line 61 of file qstyleoption.h.

◆ fontMetrics

QFontMetrics QStyleOption::fontMetrics

Definition at line 63 of file qstyleoption.h.

◆ palette

QPalette QStyleOption::palette

Definition at line 64 of file qstyleoption.h.

◆ rect

QRect QStyleOption::rect

Definition at line 62 of file qstyleoption.h.

◆ state

QStyle::State QStyleOption::state

Definition at line 60 of file qstyleoption.h.

◆ styleObject

QObject* QStyleOption::styleObject

Definition at line 65 of file qstyleoption.h.

◆ type

int QStyleOption::type

Definition at line 59 of file qstyleoption.h.

◆ version

int QStyleOption::version

Definition at line 58 of file qstyleoption.h.


The documentation for this class was generated from the following files: