内容表

上一话题

QStyleHintReturnVariant

下一话题

QStyleOptionButton

QStyleOption

QStyleOption class stores the parameters used by QStyle 函数。 更多

Inheritance diagram of PySide2.QtWidgets.QStyleOption

继承者: QStyleOptionButton , QStyleOptionComboBox , QStyleOptionComplex , QStyleOptionDockWidget , QStyleOptionFocusRect , QStyleOptionFrame , QStyleOptionGraphicsItem , QStyleOptionGroupBox , QStyleOptionHeader , QStyleOptionMenuItem , QStyleOptionProgressBar , QStyleOptionRubberBand , QStyleOptionSizeGrip , QStyleOptionSlider , QStyleOptionSpinBox , QStyleOptionTab , QStyleOptionTabBarBase , QStyleOptionTabWidgetFrame , QStyleOptionTitleBar , QStyleOptionToolBar , QStyleOptionToolBox , QStyleOptionToolButton , QStyleOptionViewItem

概要

函数

详细描述

QStyleOption and its subclasses contain all the information that QStyle functions need to draw a graphical element.

出于性能原因,有很少成员函数,且对成员变量的访问是直接的 (即:使用 . 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 Qt’s extensive use of 隐式共享 for types such as QString , QPalette ,和 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:

def paintEvent(self, qpaintevent):
    option = QStyleOptionButton()
    option.initFrom(self)
    if isDown():
        option.state = QStyle.State_Sunken
    else:
        option.state = QStyle.State_Raised
    if self.isDefault():
        option.features = option.features or QStyleOptionButton.DefaultButton
    option.text = self.text()
    option.icon = self.icon()
    painter = QPainter(self)
    self.style().drawControl(QStyle.CE_PushButton, option, painter, self)
											

In our example, the control is a CE_PushButton , and according to the 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:

def drawPrimitive(self, element, option, painter, widget):
    if element == self.PE_FrameFocusRect:
        focusRectOption =  QStyleOptionFocusRect(option)
        if focusRectOption:
            # ...
    # ...
											

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

对于可以如何使用样式选项的演示范例,见 样式 范例。

class QStyleOption ( other )

QStyleOption([version=QStyleOption.Version[, type=SO_Default]])

param type

int

param other

QStyleOption

param version

int

构造副本为 other .

构造 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.

state 成员变量被初始化为 State_None .

另请参阅

version type

PySide2.QtWidgets.QStyleOption. OptionType

此枚举被内部使用,通过 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.

常量

描述

QStyleOption.SO_Button

QStyleOptionButton

QStyleOption.SO_ComboBox

QStyleOptionComboBox

QStyleOption.SO_Complex

QStyleOptionComplex

QStyleOption.SO_Default

QStyleOption

QStyleOption.SO_DockWidget

QStyleOptionDockWidget

QStyleOption.SO_FocusRect

QStyleOptionFocusRect

QStyleOption.SO_Frame

QStyleOptionFrame

QStyleOption.SO_GraphicsItem

QStyleOptionGraphicsItem

QStyleOption.SO_GroupBox

QStyleOptionGroupBox

QStyleOption.SO_Header

QStyleOptionHeader

QStyleOption.SO_MenuItem

QStyleOptionMenuItem

QStyleOption.SO_ProgressBar

QStyleOptionProgressBar

QStyleOption.SO_RubberBand

QStyleOptionRubberBand

QStyleOption.SO_SizeGrip

QStyleOptionSizeGrip

QStyleOption.SO_Slider

QStyleOptionSlider

QStyleOption.SO_SpinBox

QStyleOptionSpinBox

QStyleOption.SO_Tab

QStyleOptionTab

QStyleOption.SO_TabBarBase

QStyleOptionTabBarBase

QStyleOption.SO_TabWidgetFrame

QStyleOptionTabWidgetFrame

QStyleOption.SO_TitleBar

QStyleOptionTitleBar

QStyleOption.SO_ToolBar

QStyleOptionToolBar

QStyleOption.SO_ToolBox

QStyleOptionToolBox

QStyleOption.SO_ToolButton

QStyleOptionToolButton

QStyleOption.SO_ViewItem

QStyleOptionViewItem (used in Interviews)

The following values are used for custom controls:

常量

描述

QStyleOption.SO_CustomBase

Reserved for custom QStyleOptions; all custom controls values must be above this value

QStyleOption.SO_ComplexCustomBase

Reserved for custom QStyleOptions; all custom complex controls values must be above this value

另请参阅

type

PySide2.QtWidgets.QStyleOption. StyleOptionType

This enum is used to hold information about the type of the style option, and is defined for each QStyleOption 子类。

常量

描述

QStyleOption.Type

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.

另请参阅

StyleOptionVersion

PySide2.QtWidgets.QStyleOption. StyleOptionVersion

This enum is used to hold information about the version of the style option, and is defined for each QStyleOption 子类。

常量

描述

QStyleOption.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.

另请参阅

StyleOptionType

PySide2.QtWidgets.QStyleOption. version
PySide2.QtWidgets.QStyleOption. type
PySide2.QtWidgets.QStyleOption. state
PySide2.QtWidgets.QStyleOption. direction
PySide2.QtWidgets.QStyleOption. rect
PySide2.QtWidgets.QStyleOption. fontMetrics
PySide2.QtWidgets.QStyleOption. palette
PySide2.QtWidgets.QStyleOption. styleObject
PySide2.QtWidgets.QStyleOption. init ( w )
参数

w QWidget

使用 initFrom ( widget ) 取而代之。

PySide2.QtWidgets.QStyleOption. initFrom ( w )
参数

w QWidget

初始化 state , direction , rect , palette , fontMetrics and styleObject 成员变量基于指定的 widget .

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