#include "DelegateCheckBox.h" #include #include #include #include #include #include "spdlog/spdlog.h" DelegateCheckBox::DelegateCheckBox(QObject *parent) : QStyledItemDelegate(parent) { } DelegateCheckBox::~DelegateCheckBox() { } QWidget* DelegateCheckBox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { // QCheckBox *checkBox = new QCheckBox(parent); // checkBox->setText("Check me"); // return checkBox; return nullptr; } void DelegateCheckBox::setEditorData(QWidget *editor, const QModelIndex &index) const { } void DelegateCheckBox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { } void DelegateCheckBox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { } void DelegateCheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { /* 获取复选框状态 */ Qt::CheckState checkState = static_cast(index.data(Qt::CheckStateRole).toInt()); /* 获取文本 */ QString text = index.data(Qt::DisplayRole).toString(); /* 自定义颜色 */ QColor backgroundColor = QColor(255, 255, 255); /* 背景色 */ QColor hightLightColor = QColor("#EEF2FF"); /* 选中高亮颜色 */ /* ------------------------ 绘制背景 ------------------------ */ if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, hightLightColor); // SPDLOG_INFO("选中状态: {}, 选中颜色: {}", true, hightLightColor.name().toStdString()); } else { painter->fillRect(option.rect, backgroundColor); // SPDLOG_INFO("选中状态: {}, 选中颜色: {}", false, opt.palette.base().color().name().toStdString()); } /* ------------------------ 绘制选择框 ------------------------ */ QRect selectIconRect = QRect(option.rect.left() + 12, option.rect.top() + (option.rect.height() - 20) / 2, 20, 20); QString selectIconPath = GetCheckboxImage(checkState); painter->drawPixmap(selectIconRect, QPixmap(selectIconPath)); // QStyleOptionButton checkBoxOption; // QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkBoxOption); // checkBoxOption.rect = QRect(option.rect.left(), option.rect.top() + (option.rect.height() - checkBoxRect.height()) / 2, checkBoxRect.width(), checkBoxRect.height()); // checkBoxOption.state = QStyle::State_Enabled | (checked ? QStyle::State_On : QStyle::State_Off); // 绘制复选框 // QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter); // 绘制文本 QRect textRect = option.rect; textRect.setLeft(selectIconRect.right() + 4); // 复选框后留点间距 painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text); } bool DelegateCheckBox::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { // if (option.state & QStyle::State_Enabled && event->type() == QEvent::MouseButtonRelease) // { // // 选中整行 // QAbstractItemView *view = qobject_cast(parent()); // if (view) // { // view->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows); // } // } return QStyledItemDelegate::editorEvent(event, model, option, index); } QString DelegateCheckBox::GetCheckboxImage(Qt::CheckState state) const { if(state == Qt::Unchecked) { return ":/icon/unchecked.png"; }else { return ":/icon/checked.png"; } // else if(state == Qt::PartiallyChecked) // { // return ":/icon/partially_checked.png"; // } // else if(state == Qt::Checked) // { // return ":/icon/checked.png"; // } return ""; }