DataTableItemDelegate.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "DataTableItemDelegate.h"
  2. #include <QComboBox>
  3. #include <QLineEdit>
  4. #include <QDebug>
  5. DataTableComboDelegate::DataTableComboDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  6. QWidget *DataTableComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  7. const QModelIndex & /* index */) const
  8. {
  9. QComboBox *w = new QComboBox(parent);
  10. w->addItem("成本型");
  11. w->addItem("效益型");
  12. return w;
  13. }
  14. void DataTableComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  15. {
  16. QString value = index.model()->data(index, Qt::EditRole).toString();
  17. QComboBox *comboBox = static_cast<QComboBox *>(editor);
  18. comboBox->setCurrentText(value);
  19. }
  20. void DataTableComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  21. {
  22. QComboBox *spinBox = static_cast<QComboBox *>(editor);
  23. QString value = spinBox->currentText();
  24. model->setData(index, value, Qt::EditRole);
  25. }
  26. void DataTableComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  27. const QModelIndex & /*index*/) const
  28. {
  29. editor->setGeometry(option.rect);
  30. }