1 #include <QStylePainter>
2 #include <QStyleOptionComboBox>
4 #include <QColorDialog>
9 ColorBox::ColorBox(QWidget
*parent
) : QWidget(parent
)
12 setSizePolicy(QSizePolicy::QSizePolicy::Minimum
, QSizePolicy::Fixed
);
15 QSize
ColorBox::sizeHint() const
26 void ColorBox::paintEvent(QPaintEvent
*event
)
30 QStylePainter
painter(this);
32 QStyleOptionComboBox option
;
33 option
.initFrom(this);
35 #if defined(Q_OS_MAC) || defined(Q_OS_WIN32)
36 painter
.setBrush(_color
);
37 painter
.drawPrimitive(QStyle::PE_Frame
, option
);
38 #else // Q_OS_MAC || Q_OS_WIN32
39 // Fallback for some broken QT4 styles that do not draw the background
40 painter
.setBrush(_color
);
41 painter
.setPen(Qt::NoPen
);
42 painter
.drawRect(event
->rect().adjusted(2, 2, -2, -2));
43 // If works (QT5 and most QT4 styles) overpaints the previous rectangle
44 option
.palette
.setBrush(QPalette::Base
, _color
);
45 painter
.drawPrimitive(QStyle::PE_PanelLineEdit
, option
);
46 painter
.drawPrimitive(QStyle::PE_FrameLineEdit
, option
);
47 #endif // Q_OS_MAC || Q_OS_WIN32
50 void ColorBox::mousePressEvent(QMouseEvent
*event
)
52 if (event
->button() != Qt::LeftButton
)
55 QColor color
= QColorDialog::getColor(_color
, this, QString(),
56 QColorDialog::ShowAlphaChannel
);
57 if (color
.isValid()) {
60 emit
colorChanged(_color
);
64 void ColorBox::setColor(const QColor
&color
)