From 15b60102a1608462dac3566048ee5744e33ead19 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 12 Apr 2024 02:29:02 +0200 Subject: [PATCH] Workaround Qt bug in Windows 11 style Pass a widget when invoking `QStyle::drawControl()`; the Windows 11 style uses the widget unconditionally. Considering the documentation explicitly states `The widget argument is optional` and the default value for the function argument is `nullptr` this is supposedly a bug in Qt. --- tray/gui/devbuttonsitemdelegate.cpp | 2 +- tray/gui/dirbuttonsitemdelegate.cpp | 4 ++-- tray/gui/downloaditemdelegate.cpp | 2 +- tray/gui/helper.cpp | 10 ++++++++++ tray/gui/helper.h | 3 +++ 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/tray/gui/devbuttonsitemdelegate.cpp b/tray/gui/devbuttonsitemdelegate.cpp index f1c0bc3..d93169c 100644 --- a/tray/gui/devbuttonsitemdelegate.cpp +++ b/tray/gui/devbuttonsitemdelegate.cpp @@ -38,7 +38,7 @@ void DevButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem initStyleOption(&opt, index); opt.text.clear(); opt.features = QStyleOptionViewItem::None; - QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter); + drawBasicItemViewItem(*painter, opt); // draw text QRectF textRect = option.rect; diff --git a/tray/gui/dirbuttonsitemdelegate.cpp b/tray/gui/dirbuttonsitemdelegate.cpp index 03abd4c..b5b5c6b 100644 --- a/tray/gui/dirbuttonsitemdelegate.cpp +++ b/tray/gui/dirbuttonsitemdelegate.cpp @@ -33,11 +33,11 @@ void DirButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem QStyledItemDelegate::paint(painter, option, index); } else { // init style options to use drawControl(), except for the text - QStyleOptionViewItem opt = option; + auto opt = option; initStyleOption(&opt, index); opt.text.clear(); opt.features = QStyleOptionViewItem::None; - QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter); + drawBasicItemViewItem(*painter, opt); // draw text auto textRect = QRectF(option.rect); diff --git a/tray/gui/downloaditemdelegate.cpp b/tray/gui/downloaditemdelegate.cpp index 4927733..808a085 100644 --- a/tray/gui/downloaditemdelegate.cpp +++ b/tray/gui/downloaditemdelegate.cpp @@ -45,7 +45,7 @@ void DownloadItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & } else { opt.text = option.fontMetrics.elidedText(opt.text, Qt::ElideMiddle, opt.rect.width() / 2 - 4); } - QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter); + drawBasicItemViewItem(*painter, opt); // draw progress bar const QAbstractItemModel *model = index.model(); diff --git a/tray/gui/helper.cpp b/tray/gui/helper.cpp index b02224e..f829b83 100644 --- a/tray/gui/helper.cpp +++ b/tray/gui/helper.cpp @@ -3,8 +3,11 @@ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 3, 0) #include #endif + +#include #include #include +#include #include namespace QtGui { @@ -30,4 +33,11 @@ void showViewMenu(const QPoint &position, const QTreeView &view, QMenu &menu) } } +void drawBasicItemViewItem(QPainter &painter, const QStyleOptionViewItem &option) +{ + if (auto *const style = option.widget ? option.widget->style() : QApplication::style()) { + style->drawControl(QStyle::CE_ItemViewItem, &option, &painter, option.widget); + } +} + } // namespace QtGui diff --git a/tray/gui/helper.h b/tray/gui/helper.h index 5cb7fd4..676efdf 100644 --- a/tray/gui/helper.h +++ b/tray/gui/helper.h @@ -12,12 +12,15 @@ #include #include +QT_FORWARD_DECLARE_CLASS(QPainter) QT_FORWARD_DECLARE_CLASS(QPoint) QT_FORWARD_DECLARE_CLASS(QMenu) +QT_FORWARD_DECLARE_CLASS(QStyleOptionViewItem) namespace QtGui { void showViewMenu(const QPoint &position, const QTreeView &view, QMenu &menu); +void drawBasicItemViewItem(QPainter &painter, const QStyleOptionViewItem &option); inline auto copyToClipboard(const QString &text) {