123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #include "DelegateCheckBox.h"
- #include <QCheckBox>
- #include <QPainter>
- #include <QApplication>
- #include <QStyleOptionViewItem>
- #include <QAbstractItemView>
- #include <QMouseEvent>
- #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<Qt::CheckState>(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 (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
- QRect checkBoxRect(option.rect.left() + 12, option.rect.top() + (option.rect.height() - 20) / 2, 20, 20);
- if (checkBoxRect.contains(mouseEvent->pos())) {
- // 直接返回 false,不处理点击
- return false;
- }
- }
- 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 "";
- }
|