Fix very bad contrast of status text on selected view items with Windows styles
This commit is contained in:
parent
29251fe422
commit
78575c088c
|
@ -45,8 +45,7 @@ void DevButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
|||
textRect.setWidth(textRect.width() - 20);
|
||||
QTextOption textOption;
|
||||
textOption.setAlignment(opt.displayAlignment);
|
||||
painter->setFont(opt.font);
|
||||
painter->setPen(opt.palette.color(opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text));
|
||||
setupPainterToDrawViewItemText(painter, opt);
|
||||
painter->drawText(textRect, displayText(index.data(Qt::DisplayRole), option.locale), textOption);
|
||||
|
||||
// draw buttons
|
||||
|
|
|
@ -44,8 +44,7 @@ void DirButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
|||
textRect.setWidth(textRect.width() - 58);
|
||||
QTextOption textOption;
|
||||
textOption.setAlignment(opt.displayAlignment);
|
||||
painter->setFont(opt.font);
|
||||
painter->setPen(opt.palette.color(opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text));
|
||||
setupPainterToDrawViewItemText(painter, opt);
|
||||
painter->drawText(textRect, displayText(index.data(Qt::DisplayRole), option.locale), textOption);
|
||||
|
||||
// draw buttons
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <QApplication>
|
||||
#include <QMenu>
|
||||
#include <QPainter>
|
||||
#include <QPoint>
|
||||
#include <QStyleOptionViewItem>
|
||||
#include <QTreeView>
|
||||
|
@ -66,4 +67,31 @@ void drawBasicItemViewItem(QPainter &painter, const QStyleOptionViewItem &option
|
|||
}
|
||||
}
|
||||
|
||||
void setupPainterToDrawViewItemText(QPainter *painter, QStyleOptionViewItem &opt)
|
||||
{
|
||||
painter->setFont(opt.font);
|
||||
|
||||
if (!(opt.state & QStyle::State_Selected)) {
|
||||
painter->setPen(opt.palette.color(QPalette::Text));
|
||||
return;
|
||||
}
|
||||
|
||||
// set pen/palette in accordance with the Windows 11 or Windows Vista style for selected items
|
||||
// note: These styles unfortunately don't just use the highlighted text color and just using it would
|
||||
// lead to a very bad contrast.
|
||||
#if defined(Q_OS_WINDOWS) && QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
|
||||
auto *const style = opt.widget ? opt.widget->style() : nullptr;
|
||||
const auto styleName = style ? style->name() : QString();
|
||||
if (styleName.compare(QLatin1String("windows11"), Qt::CaseInsensitive) == 0) {
|
||||
painter->setPen(QPen(opt.palette.buttonText().color()));
|
||||
return;
|
||||
} else if (styleName.compare(QLatin1String("windowsvista"), Qt::CaseInsensitive) == 0) {
|
||||
opt.palette.setColor(QPalette::All, QPalette::HighlightedText, opt.palette.color(QPalette::Active, QPalette::Text));
|
||||
opt.palette.setColor(QPalette::All, QPalette::Highlight, opt.palette.base().color().darker(108));
|
||||
}
|
||||
#endif
|
||||
|
||||
painter->setPen(opt.palette.color(QPalette::HighlightedText));
|
||||
}
|
||||
|
||||
} // namespace QtGui
|
||||
|
|
|
@ -29,6 +29,7 @@ public:
|
|||
|
||||
void showViewMenu(const QPoint &position, const QTreeView &view, QMenu &menu);
|
||||
void drawBasicItemViewItem(QPainter &painter, const QStyleOptionViewItem &option);
|
||||
void setupPainterToDrawViewItemText(QPainter *painter, QStyleOptionViewItem &opt);
|
||||
|
||||
inline auto copyToClipboard(const QString &text)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue