class QTaskTypesDelegate : public QItemDelegate{ public: QTaskTypesDelegate(QObject *parent) : QItemDelegate(parent) { } void setEditorData(QWidget *editor, const QModelIndex &index) const { if(index.column()==8) { QVariant value = index.model()->data( index, Qt::EditRole); QComboBox *combo = static_cast<QComboBox*>(editor); combo->setCurrentIndex(combo->findData(value)); } else { QItemDelegate::setEditorData(editor, index); } } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { if(index.column()==8) { QComboBox *combo = static_cast<QComboBox*>(editor); QVariant value = combo->itemData(combo->currentIndex()); model->setData(index, value); } else { QItemDelegate::setModelData(editor,model,index); } }};
QSqlQueryModel *taskTypeModel = new QSqlQueryModel();taskTypeModel->setQuery("SELECT * FROM task_types");for(int row=0; row<taskTypeModel->rowCount(); ++row) { QSqlRecord record = taskTypeModel->record(row); comboBoxType->addItem(record.value("name").toString(), record.value("id"));}
mapper->setItemDelegate(new QTaskTypesDelegate(this));mapper->addMapping(comboBoxType,8);
void QComboBox::setModel ( QAbstractItemModel * model )
QSqlQueryModel *taskTypeModel = new QSqlQueryModel();taskTypeModel->setQuery("SELECT id,name FROM task_types");comboBoxType->setModel(taskTypeModel);comboBoxType->setModelColumn(1);
model->data(model->index(combo->currentIndex(),0)).toInt()