CopperSpice API  1.8.0
QStyleOption Class Reference

The QStyleOption class stores the parameters used by methods in QStyle. More...

Inheritance diagram for QStyleOption:
QStyleOptionButton QStyleOptionComplex QStyleOptionDockWidget QStyleOptionFocusRect QStyleOptionFrame QStyleOptionGraphicsItem QStyleOptionHeader QStyleOptionMenuItem QStyleOptionProgressBar QStyleOptionRubberBand QStyleOptionTab QStyleOptionTabBarBase QStyleOptionTabWidgetFrame QStyleOptionToolBar QStyleOptionToolBox QStyleOptionViewItem

Public Types

enum  OptionType
 
enum  StyleOptionType
 
enum  StyleOptionVersion
 

Public Methods

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

Public Members

int type
 
int version
 

Related Functions

These are not member functions

qstyleoption_cast (const QStyleOption *option)
 
qstyleoption_cast (QStyleOption *option)
 

Detailed Description

The QStyleOption class stores the parameters used by methods in QStyle. QStyleOption and its subclasses contain all the information QStyle needs to draw a graphical element. For performance reasons there are few methods and the data is stored in public data members.

Example

The following code shows how to use a QStyleOptionButton to paint a push button. The control is a QStyle::CE_PushButton and the QStyle::drawControl() uses a QStyleOptionButton.

void MyPushButton::paintEvent(QPaintEvent *) {
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);
}

When reimplementing QStyle methods which use a QStyleOption parameter, you may need to cast the QStyleOption to the child data type. The qstyleoption_cast() will return a nullptr if the object is not of the correct type.

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) {
// do something
}
}
}
See also
QStyle, QStylePainter

Member Enumeration Documentation

This enum is used internally by QStyleOption and its subclasses

ConstantValueDescription
QStyleOption::SO_Button2QStyleOptionButton
QStyleOption::SO_ComboBox?QStyleOptionComboBox
QStyleOption::SO_Complex0xf0000QStyleOptionComplex
QStyleOption::SO_Default0QStyleOption
QStyleOption::SO_DockWidget9QStyleOptionDockWidget
QStyleOption::SO_FocusRect1QStyleOptionFocusRect
QStyleOption::SO_Frame5QStyleOptionFrame
QStyleOption::SO_GraphicsItem15QStyleOptionGraphicsItem
QStyleOption::SO_GroupBox?QStyleOptionGroupBox
QStyleOption::SO_Header8QStyleOptionHeader
QStyleOption::SO_MenuItem4QStyleOptionMenuItem
QStyleOption::SO_ProgressBar6QStyleOptionProgressBar
QStyleOption::SO_RubberBand13QStyleOptionRubberBand
QStyleOption::SO_SizeGrip?QStyleOptionSizeGrip
QStyleOption::SO_Slider?QStyleOptionSlider
QStyleOption::SO_SpinBox?QStyleOptionSpinBox
QStyleOption::SO_Tab3QStyleOptionTab
QStyleOption::SO_TabBarBase12QStyleOptionTabBarBase
QStyleOption::SO_TabWidgetFrame11QStyleOptionTabWidgetFrame
QStyleOption::SO_TitleBar?QStyleOptionTitleBar
QStyleOption::SO_ToolBar14QStyleOptionToolBar
QStyleOption::SO_ToolBox7QStyleOptionToolBox
QStyleOption::SO_ToolButton?QStyleOptionToolButton
QStyleOption::SO_ViewItem10QStyleOptionViewItem (used in Interviews)

The following values are used for custom controls:

ConstantValueDescription
QStyleOption::SO_CustomBase0xf00 Reserved for custom QStyleOptions; all custom controls values must be above this value
QStyleOption::SO_ComplexCustomBase0xf000000 Reserved for custom QStyleOptions; all custom complex controls values must be above this value
See also
QStyleOption::StyleOptionType, qstyleoption_cast()

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

ConstantValueDescription
QStyleOption::Type SO_Default The type of style option provided by this class
See also
QStyleOption::StyleOptionVersion, qstyleoption_cast()

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

ConstantValueDescription
QStyleOption::Version 1 Version 1
See also
QStyleOption::StyleOptionType, qstyleoption_cast()

Constructor & Destructor 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::StyleOptionType, QStyleOption::StyleOptionVersion
QStyleOption::QStyleOption ( const QStyleOption &  other)

Constructs a copy of other.

QStyleOption::~QStyleOption ( )

Destroys this style option object.

Method Documentation

void QStyleOption::initFrom ( const QWidget widget)

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

See also
QWidget::layoutDirection(), QWidget::rect(), QWidget::palette(), QWidget::fontMetrics()
QStyleOption & QStyleOption::operator= ( const QStyleOption &  other)

Assign other to this QStyleOption.

Friends And Related Function Documentation

T qstyleoption_cast ( const QStyleOption *  option)
related

Returns a T or a default constructed T depending on the type and version of the given 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) {
// do something
}
}
// do something
}
See also
QStyleOption::StyleOptionType, QStyleOption::StyleOptionVersion
T qstyleoption_cast ( QStyleOption *  option)
related

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

Member Data Documentation

QStyleOption::type

This variable holds the type of the style option. The default value is SO_Default.

See also
QStyleOption::OptionType
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.