From 0d14ad493654c1170185d536ea0cbc40b30727cc Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Mon, 2 Jun 2025 20:27:48 +0200 Subject: [PATCH] Gui: Make checkbox in the property view QSS aware --- .../propertyeditor/PropertyItemDelegate.cpp | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Gui/propertyeditor/PropertyItemDelegate.cpp b/src/Gui/propertyeditor/PropertyItemDelegate.cpp index 5d7261b88a..6bebaf5ae7 100644 --- a/src/Gui/propertyeditor/PropertyItemDelegate.cpp +++ b/src/Gui/propertyeditor/PropertyItemDelegate.cpp @@ -107,37 +107,42 @@ void PropertyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & if (index.column() == 1 && property && dynamic_cast(property)) { bool checked = index.data(Qt::EditRole).toBool(); + bool readonly = property->isReadOnly(); + + QStyle* style = option.widget ? option.widget->style() : QApplication::style(); + QPalette palette = option.widget ? option.widget->palette() : QApplication::palette(); + QStyleOptionButton checkboxOption; - if (property->isReadOnly()) { - checkboxOption.state |= QStyle::State_ReadOnly; - } else { - checkboxOption.state |= QStyle::State_Enabled; - } + + checkboxOption.state |= readonly ? QStyle::State_ReadOnly : QStyle::State_Enabled; checkboxOption.state |= checked ? QStyle::State_On : QStyle::State_Off; + + // draw the item (background etc.) + style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget); + // Draw the checkbox - checkboxOption.rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxOption); - int leftSpacing = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr); + checkboxOption.rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxOption, option.widget); + int leftSpacing = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, option.widget); + QRect checkboxRect = QStyle::alignedRect( option.direction, Qt::AlignVCenter, checkboxOption.rect.size(), option.rect.adjusted(leftSpacing, 0, -leftSpacing, 0) ); checkboxOption.rect = checkboxRect; - QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkboxOption, painter); - // Draw a bright border on the checkbox to stand out - QColor borderColor = QApplication::palette().color(QPalette::BrightText); - painter->setPen(borderColor); - painter->drawRect(checkboxOption.rect.adjusted(0, 0, -1, -1)); + + style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkboxOption, painter, option.widget); + // Draw the label of the checkbox QString labelText = checked ? tr("Yes") : tr("No"); - int spacing = QApplication::style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, nullptr); + int spacing = style->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, nullptr, option.widget); QRect textRect( checkboxOption.rect.right() + spacing, checkboxOption.rect.top(), option.rect.right() - (checkboxOption.rect.right() + spacing), checkboxOption.rect.height() ); - painter->setPen(option.palette.color(QPalette::Text)); + painter->setPen(palette.color(QPalette::Text)); painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, labelText); } else {