DelegateCheckBox.cpp 4.0 KB

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