query.exec("select id, surname, name, patronymic, field1, field2...");while (query.next()){ const QString key = query.value(1).toString() + " " + query.value(2).toString() + " " + query.value(3).toString() + " " + query.value(4).toString() + " " + query.value(5).toString(); map[key] = query.value(0).toInt(); }myTableView->setItemDelegateForColumn(column_number, new MyDelegate(map));
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ int i = index.model()->data(index, Qt::DisplayRole).toInt(); drawDisplay(painter, option, option.rect, m_map.key(i)); drawFocus(painter, option, option.rect);}QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QComboBox *combobox = new QComboBox(parent); QMapIterator<QString, int> i(m_map); combobox->setSizeAdjustPolicy(QComboBox::AdjustToContents); while (i.hasNext()) { i.next(); combobox->addItem(i.key(), i.value()); } combobox->installEventFilter(const_cast<DonorDelegate*>(this)); return combobox;}void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ int i = index.model()->data(index, Qt::DisplayRole).toInt(); QComboBox *combobox = qobject_cast<QComboBox*>(editor); combobox->setSizeAdjustPolicy(QComboBox::AdjustToContents); combobox->setCurrentIndex(combobox->findData(i));}void DonorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QComboBox *combobox = qobject_cast<QComboBox*>(editor); combobox->setSizeAdjustPolicy(QComboBox::AdjustToContents); QVariant i = combobox->itemData(combobox->currentIndex()); model->setData(index, i);}
model->sort( колонка_с_id, Qt::AscendingOrder );
combobox->addItem(i.key(), i.value());