Merge pull request #21749 from kadet1090/qss-aware-checkbox-property-panel

This commit is contained in:
Benjamin Nauck
2025-06-04 12:31:49 +02:00
committed by GitHub
3 changed files with 23 additions and 14 deletions

View File

@@ -2278,6 +2278,8 @@ QListView::indicator,
QTableView::indicator, QTableView::indicator,
QColumnView::indicator { QColumnView::indicator {
border: 1px solid #ffffff; border: 1px solid #ffffff;
width: 12px;
height: 12px;
} }
QTreeView::indicator:unchecked:disabled, QTreeView::indicator:unchecked:disabled,

View File

@@ -2271,6 +2271,8 @@ QListView::indicator,
QTableView::indicator, QTableView::indicator,
QColumnView::indicator { QColumnView::indicator {
border: 1px solid #1c1c1c; border: 1px solid #1c1c1c;
width: 12px;
height: 12px;
} }
QTreeView::indicator:unchecked:disabled, QTreeView::indicator:unchecked:disabled,

View File

@@ -107,37 +107,42 @@ void PropertyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
if (index.column() == 1 && property && dynamic_cast<PropertyBoolItem*>(property)) { if (index.column() == 1 && property && dynamic_cast<PropertyBoolItem*>(property)) {
bool checked = index.data(Qt::EditRole).toBool(); 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; QStyleOptionButton checkboxOption;
if (property->isReadOnly()) {
checkboxOption.state |= QStyle::State_ReadOnly; checkboxOption.state |= readonly ? QStyle::State_ReadOnly : QStyle::State_Enabled;
} else {
checkboxOption.state |= QStyle::State_Enabled;
}
checkboxOption.state |= checked ? QStyle::State_On : QStyle::State_Off; 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 // Draw the checkbox
checkboxOption.rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxOption); checkboxOption.rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxOption, option.widget);
int leftSpacing = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr); int leftSpacing = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, option.widget);
QRect checkboxRect = QStyle::alignedRect( QRect checkboxRect = QStyle::alignedRect(
option.direction, Qt::AlignVCenter, option.direction, Qt::AlignVCenter,
checkboxOption.rect.size(), checkboxOption.rect.size(),
option.rect.adjusted(leftSpacing, 0, -leftSpacing, 0) option.rect.adjusted(leftSpacing, 0, -leftSpacing, 0)
); );
checkboxOption.rect = checkboxRect; checkboxOption.rect = checkboxRect;
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkboxOption, painter);
// Draw a bright border on the checkbox to stand out style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkboxOption, painter, option.widget);
QColor borderColor = QApplication::palette().color(QPalette::BrightText);
painter->setPen(borderColor);
painter->drawRect(checkboxOption.rect.adjusted(0, 0, -1, -1));
// Draw the label of the checkbox // Draw the label of the checkbox
QString labelText = checked ? tr("Yes") : tr("No"); 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( QRect textRect(
checkboxOption.rect.right() + spacing, checkboxOption.rect.right() + spacing,
checkboxOption.rect.top(), checkboxOption.rect.top(),
option.rect.right() - (checkboxOption.rect.right() + spacing), option.rect.right() - (checkboxOption.rect.right() + spacing),
checkboxOption.rect.height() checkboxOption.rect.height()
); );
painter->setPen(option.palette.color(QPalette::Text)); painter->setPen(palette.color(QPalette::Text));
painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, labelText); painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, labelText);
} }
else { else {