From 4cf027df5876da7c1f9a53eea5682fb54dc8c63b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 6 Dec 2016 21:01:06 +0100 Subject: [PATCH] Fixed widget rendering issues --- src/colorbox.cpp | 12 ++++++------ src/stylecombobox.cpp | 14 ++++++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/colorbox.cpp b/src/colorbox.cpp index 86314de8..95e3cc79 100644 --- a/src/colorbox.cpp +++ b/src/colorbox.cpp @@ -30,20 +30,20 @@ void ColorBox::paintEvent(QPaintEvent *event) QStylePainter painter(this); QStyleOptionComboBox option; - option.initFrom(this); + option.initFrom(this); #if defined(Q_OS_MAC) || defined(Q_OS_WIN32) painter.setBrush(_color); painter.drawPrimitive(QStyle::PE_Frame, option); #else // Q_OS_MAC || Q_OS_WIN32 -#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) + // Fallback for some broken QT4 styles that do not draw the background painter.setBrush(_color); - painter.drawRect(event->rect().adjusted(-1, -1, 0, 0)); - painter.drawPrimitive(QStyle::PE_FrameLineEdit, option); -#else // QT 5 + painter.setPen(Qt::NoPen); + painter.drawRect(event->rect().adjusted(2, 2, -2, -2)); + // If works (QT5 and most QT4 styles) overpaints the previous rectangle option.palette.setBrush(QPalette::Base, _color); + painter.drawPrimitive(QStyle::PE_PanelLineEdit, option); painter.drawPrimitive(QStyle::PE_FrameLineEdit, option); -#endif // QT 5 #endif // Q_OS_MAC || Q_OS_WIN32 } diff --git a/src/stylecombobox.cpp b/src/stylecombobox.cpp index c8921661..c256f540 100644 --- a/src/stylecombobox.cpp +++ b/src/stylecombobox.cpp @@ -3,6 +3,10 @@ #include #include "stylecombobox.h" + +#define MIN_LINE_LENGTH 50 +#define LINE_WIDTH_RATIO 7 + #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) StyleComboBox::StyleComboBox(QWidget *parent) : QComboBox(parent) @@ -11,7 +15,7 @@ StyleComboBox::StyleComboBox(QWidget *parent) : QComboBox(parent) Qt::DashDotLine, Qt::DashDotDotLine}; QSize is = iconSize(); - setIconSize(QSize(sizeHint().width(), is.height())); + setIconSize(QSize(MIN_LINE_LENGTH, is.height())); is = iconSize(); for (size_t i = 0; i < ARRAY_SIZE(styles); i++) { @@ -19,7 +23,7 @@ StyleComboBox::StyleComboBox(QWidget *parent) : QComboBox(parent) pm.fill(Qt::transparent); QBrush brush(Qt::black); - QPen pen(brush, is.height() / 7, styles[i]); + QPen pen(brush, is.height() / LINE_WIDTH_RATIO, styles[i]); QPainter painter(&pm); painter.setPen(pen); @@ -42,7 +46,8 @@ void StyleComboBox::setValue(Qt::PenStyle value) void StyleComboBox::resizeEvent(QResizeEvent *event) { QSize is = iconSize(); - setIconSize(QSize(event->size().width() - 30, is.height())); + setIconSize(QSize(qMax(event->size().width() - 40, MIN_LINE_LENGTH), + is.height())); is = iconSize(); for (int i = 0; i < count(); i++) { @@ -50,7 +55,8 @@ void StyleComboBox::resizeEvent(QResizeEvent *event) pm.fill(Qt::transparent); QBrush brush(Qt::black); - QPen pen(brush, is.height() / 7, (Qt::PenStyle) itemData(i).toInt()); + QPen pen(brush, is.height() / LINE_WIDTH_RATIO, + (Qt::PenStyle) itemData(i).toInt()); QPainter painter(&pm); painter.setPen(pen);