DelegateCheckBox.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "DelegateCheckBox.h"
  2. #include <QCheckBox>
  3. #include <QPainter>
  4. #include <QApplication>
  5. #include <QStyleOptionViewItem>
  6. #include <QAbstractItemView>
  7. #include <QMouseEvent>
  8. #include "spdlog/spdlog.h"
  9. DelegateCheckBox::DelegateCheckBox(QObject *parent)
  10. : QStyledItemDelegate(parent)
  11. {
  12. }
  13. DelegateCheckBox::~DelegateCheckBox()
  14. {
  15. }
  16. QWidget* DelegateCheckBox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  17. {
  18. // QCheckBox *checkBox = new QCheckBox(parent);
  19. // checkBox->setText("Check me");
  20. // return checkBox;
  21. return nullptr;
  22. }
  23. void DelegateCheckBox::setEditorData(QWidget *editor, const QModelIndex &index) const
  24. {
  25. }
  26. void DelegateCheckBox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  27. {
  28. }
  29. void DelegateCheckBox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
  30. {
  31. }
  32. void DelegateCheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  33. {
  34. /* 获取复选框状态 */
  35. Qt::CheckState checkState = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
  36. /* 获取文本 */
  37. QString text = index.data(Qt::DisplayRole).toString();
  38. /* 自定义颜色 */
  39. QColor backgroundColor = QColor(255, 255, 255); /* 背景色 */
  40. QColor hightLightColor = QColor("#EEF2FF"); /* 选中高亮颜色 */
  41. /* ------------------------ 绘制背景 ------------------------ */
  42. if (option.state & QStyle::State_Selected)
  43. {
  44. painter->fillRect(option.rect, hightLightColor);
  45. // SPDLOG_INFO("选中状态: {}, 选中颜色: {}", true, hightLightColor.name().toStdString());
  46. } else
  47. {
  48. painter->fillRect(option.rect, backgroundColor);
  49. // SPDLOG_INFO("选中状态: {}, 选中颜色: {}", false, opt.palette.base().color().name().toStdString());
  50. }
  51. /* ------------------------ 绘制选择框 ------------------------ */
  52. QRect selectIconRect = QRect(option.rect.left() + 12, option.rect.top() + (option.rect.height() - 20) / 2, 20, 20);
  53. QString selectIconPath = GetCheckboxImage(checkState);
  54. painter->drawPixmap(selectIconRect, QPixmap(selectIconPath));
  55. // QStyleOptionButton checkBoxOption;
  56. // QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkBoxOption);
  57. // checkBoxOption.rect = QRect(option.rect.left(), option.rect.top() + (option.rect.height() - checkBoxRect.height()) / 2, checkBoxRect.width(), checkBoxRect.height());
  58. // checkBoxOption.state = QStyle::State_Enabled | (checked ? QStyle::State_On : QStyle::State_Off);
  59. // 绘制复选框
  60. // QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter);
  61. // 绘制文本
  62. QRect textRect = option.rect;
  63. textRect.setLeft(selectIconRect.right() + 4); // 复选框后留点间距
  64. painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text);
  65. }
  66. bool DelegateCheckBox::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
  67. {
  68. // 禁用点击复选框区域
  69. if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
  70. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  71. QRect checkBoxRect(option.rect.left() + 12, option.rect.top() + (option.rect.height() - 20) / 2, 20, 20);
  72. if (checkBoxRect.contains(mouseEvent->pos())) {
  73. // 直接返回 false,不处理点击
  74. return false;
  75. }
  76. }
  77. return QStyledItemDelegate::editorEvent(event, model, option, index);
  78. }
  79. QString DelegateCheckBox::GetCheckboxImage(Qt::CheckState state) const
  80. {
  81. if(state == Qt::Unchecked)
  82. {
  83. return ":/icon/unchecked.png";
  84. }else {
  85. return ":/icon/checked.png";
  86. }
  87. // else if(state == Qt::PartiallyChecked)
  88. // {
  89. // return ":/icon/partially_checked.png";
  90. // }
  91. // else if(state == Qt::Checked)
  92. // {
  93. // return ":/icon/checked.png";
  94. // }
  95. return "";
  96. }