GreyClusteringItemDelegate.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "GreyClusteringItemDelegate.h"
  2. #include <QMessageBox>
  3. GreyClusteringItemDelegate::GreyClusteringItemDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  4. QWidget *GreyClusteringItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  5. const QModelIndex & /* index */) const
  6. {
  7. RangeSpin *editor = new RangeSpin(parent);
  8. return editor;
  9. }
  10. void GreyClusteringItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  11. {
  12. QString value = index.model()->data(index, Qt::EditRole).toString();
  13. RangeSpin *spinBox = static_cast<RangeSpin *>(editor);
  14. spinBox->setValue(value);
  15. }
  16. void GreyClusteringItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  17. const QModelIndex &index) const
  18. {
  19. RangeSpin *spinBox = static_cast<RangeSpin *>(editor);
  20. if (!spinBox->valid()) {
  21. QMessageBox::warning(nullptr, "警告", QString("设置的值%1区间错误,请重新设置!").arg(spinBox->result()));
  22. return;
  23. }
  24. QString value = spinBox->result();
  25. model->setData(index, value, Qt::EditRole);
  26. }
  27. void GreyClusteringItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  28. const QModelIndex & /* index */) const
  29. {
  30. QRect rect = option.rect;
  31. rect.adjust(-10, -10, 10, 10);
  32. editor->setGeometry(rect);
  33. }