本页内容

QStylePainter Class

QStylePainter 类是一个用于在小部件内部绘制 `QStyle ` 元素的便捷类。更多内容...

头文件: #include <QStylePainter>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承自: QPainter

公共函数

QStylePainter()
QStylePainter(QWidget *widget)
QStylePainter(QPaintDevice *pd, QWidget *widget)
bool begin(QWidget *widget)
bool begin(QPaintDevice *pd, QWidget *widget)
void drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex &option)
void drawControl(QStyle::ControlElement ce, const QStyleOption &option)
void drawItemPixmap(const QRect &rect, int flags, const QPixmap &pixmap)
void drawItemText(const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole = QPalette::NoRole)
void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption &option)
QStyle *style() const

详细描述

QStylePainter 继承自QPainter ,并在QStyle 的 API 基础上实现了一组高级的draw...() 函数。使用 QStylePainter 的优势在于参数列表会大幅简化。QStyle 对象必须能够使用任何绘图器在任何小部件上绘制(因为应用程序通常有一个由所有小部件共享的QStyle 对象),而QStylePainter是在初始化时指定一个小部件的,因此无需在每次函数调用时都指定QWidget 、QPainter 和QStyle 。

直接使用QStyle 的示例:

void MyWidget::paintEvent(QPaintEvent * /* event */)
{
    QPainter painter(this);

    QStyleOptionFocusRect option;
    option.initFrom(this);
    option.backgroundColor = palette().color(QPalette::Window);

    style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, this);
}

使用 QStylePainter 的示例:

void MyWidget::paintEvent(QPaintEvent * /* event */)
{
    QStylePainter painter(this);

    QStyleOptionFocusRect option;
    option.initFrom(this);
    option.backgroundColor = palette().color(QPalette::Window);

    painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
}

另请参阅 QStyle 和QStyleOption 。

成员函数文档

QStylePainter::QStylePainter()

创建一个 QStylePainter。

[explicit] QStylePainter::QStylePainter(QWidget *widget)

使用小部件widget 作为其绘制设备来构建一个QStylePainter。

QStylePainter::QStylePainter(QPaintDevice *pd, QWidget *widget)

使用pd 作为其绘制设备,并采用widget 中的属性,构建一个QStylePainter。

bool QStylePainter::begin(QWidget *widget)

在指定的widget 上开始绘制操作。如果绘制器已准备就绪,则返回true ;否则返回false 。

此方法由接受QWidget 参数的构造函数自动调用。

bool QStylePainter::begin(QPaintDevice *pd, QWidget *widget)

在绘图设备pd 上开始绘制操作,就好像它是widget 一样。

该函数由一个接受QPaintDevice 和QWidget 作为参数的构造函数自动调用。

这是一个重载函数。

void QStylePainter::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex &option)

使用小部件的样式来绘制由QStyleOptionComplex option 指定的复杂控件cc 。

另请参阅 QStyle::drawComplexControl()。

void QStylePainter::drawControl(QStyle::ControlElement ce, const QStyleOption &option)

使用小部件的样式来绘制由QStyleOption option 指定的控件元素ce 。

另请参阅 QStyle::drawControl()。

void QStylePainter::drawItemPixmap(const QRect &rect, int flags, const QPixmap &pixmap)

在矩形rect 中绘制位图pixmap 。该位图将根据flags 进行对齐。

另请参阅 QStyle::drawItemPixmap() 和Qt::Alignment 。

void QStylePainter::drawItemText(const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole = QPalette::NoRole)

在矩形rect 和调色板pal 中绘制text 。文本的对齐和换行方式遵循flags 。

笔色通过textRole 指定。enabled 布尔值表示该项是否启用;在重写此方法时,该布尔值应影响该项的绘制方式。

另请参阅 QStyle::drawItemText() 和Qt::Alignment 。

void QStylePainter::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption &option)

使用小部件的样式绘制由QStyleOption option 指定的原始元素pe 。

另请参阅 QStyle::drawPrimitive()。

QStyle *QStylePainter::style() const

返回QStylePainter 当前使用的样式。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.