This is the code I ended up with (from the QSqlRelationalDelegate 's code), and works!
Any comments are welcome.
int positionColumn = 2;
qDebug()<< "BookDelegate.createEditor index.column()=" << (index.column()) << " option=" << option ;
if (index.column() == positionColumn){
QSqlTableModel *childModel
= sqlModel ? sqlModel
->relationModel
(index.
column()) : nullptr;
if (!childModel)
return QStyledItemDelegate::createEditor(parent, option, index);
combo->setModel(childModel);
combo->setModelColumn(1);
combo->installEventFilter(const_cast<BookDelegate *>(this));
childModel->select();
return combo;
}
int positionColumn = 2;
qDebug()<< "BookDelegate.createEditor index.column()=" << (index.column()) << " option=" << option ;
if (index.column() == positionColumn){
const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
if (!childModel)
return QStyledItemDelegate::createEditor(parent, option, index);
QComboBox *combo = new QComboBox(parent);
combo->setModel(childModel);
combo->setModelColumn(1);
combo->installEventFilter(const_cast<BookDelegate *>(this));
childModel->select();
return combo;
}
To copy to clipboard, switch view to plain text mode
Bookmarks