1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5
6#include "customstyle.h"
7
8CustomStyle::CustomStyle(const QWidget *widget)
9{
10//! [0]
11 const QSpinBox *spinBox = qobject_cast<const QSpinBox *>(object: widget);
12 if (spinBox) {
13//! [0] //! [1]
14 }
15//! [1]
16}
17
18//! [2]
19void CustomStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
20 QPainter *painter, const QWidget *widget) const
21{
22 if (element == PE_IndicatorSpinUp || element == PE_IndicatorSpinDown) {
23 QPolygon points(3);
24 int x = option->rect.x();
25 int y = option->rect.y();
26 int w = option->rect.width() / 2;
27 int h = option->rect.height() / 2;
28 x += (option->rect.width() - w) / 2;
29 y += (option->rect.height() - h) / 2;
30
31 if (element == PE_IndicatorSpinUp) {
32 points[0] = QPoint(x, y + h);
33 points[1] = QPoint(x + w, y + h);
34 points[2] = QPoint(x + w / 2, y);
35 } else { // PE_SpinBoxDown
36 points[0] = QPoint(x, y);
37 points[1] = QPoint(x + w, y);
38 points[2] = QPoint(x + w / 2, y + h);
39 }
40
41 if (option->state & State_Enabled) {
42 painter->setPen(option->palette.mid().color());
43 painter->setBrush(option->palette.buttonText());
44 } else {
45 painter->setPen(option->palette.buttonText().color());
46 painter->setBrush(option->palette.mid());
47 }
48 painter->drawPolygon(polygon: points);
49 } else {
50 QProxyStyle::drawPrimitive(element, option, painter, widget);
51//! [2] //! [3]
52 }
53//! [3] //! [4]
54}
55//! [4]
56

source code of qtbase/src/widgets/doc/snippets/customstyle/customstyle.cpp