#include "DataTableItemDelegate.h" #include #include #include DataTableComboDelegate::DataTableComboDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *DataTableComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const { QComboBox *w = new QComboBox(parent); w->addItem("成本型"); w->addItem("效益型"); return w; } void DataTableComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString value = index.model()->data(index, Qt::EditRole).toString(); QComboBox *comboBox = static_cast(editor); comboBox->setCurrentText(value); } void DataTableComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *spinBox = static_cast(editor); QString value = spinBox->currentText(); model->setData(index, value, Qt::EditRole); } void DataTableComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const { editor->setGeometry(option.rect); }