CopperSpice API  1.7.2
QStyleOption Class Reference

The QStyleOption class stores the parameters used by QStyle functions. 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)
 

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 QStyle functions. 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 low-level feel 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 the CopperSpice 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 *) {
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 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.

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() function will return nullptr if the object to which option points is not of the correct type.

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
type, 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::TypeSO_DefaultThe type of style option provided (SO_Default for this class).
See also
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::Version11
See also
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
version, type
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 0 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) {
// ...
}
}
// ...
}
See also
QStyleOption::type, QStyleOption::version
T qstyleoption_cast ( QStyleOption *  option)
related

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