#include "comboboxdelegate.h" #include #include "QStringListModel" ComboBoxDelegate::ComboBoxDelegate(QObject *parent) : QItemDelegate(parent) { } ComboBoxDelegate::~ComboBoxDelegate() { } void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QItemDelegate::paint(painter, option, index); } QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { return QItemDelegate::sizeHint(option, index); } QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.isValid() && index.column() == COMBOXCOL) { QComboBox *editor = new QComboBox(parent); editor->setEditable(true); editor->installEventFilter(const_cast(this)); return editor; } else { return QItemDelegate::createEditor(parent, option, index); } } void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { if (index.isValid() && index.column() == COMBOXCOL) { QString value = index.model()->data(index, Qt::DisplayRole).toString(); // QStringListModel* model = new QStringListModel; // QStringList stringlist; // stringlist << "Test1" << "Test2" << "Test3"; // model->setStringList(stringlist); // ui->comboBox->setModel(model); QComboBox *combox = static_cast(editor); combox->addItem("上料"); combox->addItem("下料"); combox->addItem("功率测量"); combox->addItem("对零"); combox->addItem("寻边"); combox->addItem("扫描"); combox->addItem("打标"); combox->addItem("加工"); combox->addItem("晶锭预测量"); combox->addItem("小面加工"); combox->addItem("DD马达"); combox->setCurrentText(value); //combox->setEditable(false); } else { QItemDelegate::setEditorData(editor, index); } } void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { if (index.isValid() && index.column() == COMBOXCOL) { QComboBox *combox = static_cast(editor); model->setData(index, combox->currentText()); int row = model->rowCount(); for (int i = 0; i < row; i++) { QModelIndex index = model->index(i, 0); QModelIndex index1 = model->index(i, 1); QModelIndex index2 = model->index(i, 2); QString strValue = model->data(index).toString(); if (strValue != "加工") { model->setData(index2, ""); } if ((strValue == "加工") || (strValue == "功率测量") || (strValue == "DD马达")) { } else { model->setData(index1, ""); } } } else { QItemDelegate::setModelData(editor, model, index); } }