qt4.8
I have a relational table model, with editable relations that work.
The table is filtered, and the default relational delegate
collects all possible values from the database as combo options.
I would like to restrict the available options to either:
a) the option set defined by the table's filter - i.e. what's available in the current table
OR
b) manually-generated list (of relations in the current table )
either one will do.
I attempted a specialized createEditor function to
create an editor with a fixed/generated set of options.
{
Q_UNUSED(option);
QSqlTableModel *childModel
= sqlModel ? sqlModel
->relationModel
(index.
column()) : 0;
if (!childModel)
combo->setDuplicatesEnabled(false);
combo->setModel(childModel);
combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
combo->clear(); // get rid of stuff from model,
combo->addItems(*_relevant_teams); // and put in options from a subset ( generated list )
combo->installEventFilter(const_cast<TeamSelectDelegate *>(this));
return combo;
QWidget *TeamSelectDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
Q_UNUSED(option);
const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
if (!childModel)
return QItemDelegate::createEditor(parent, option, index);
QComboBox *combo = new QComboBox(parent);
combo->setDuplicatesEnabled(false);
combo->setModel(childModel);
combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
combo->clear(); // get rid of stuff from model,
combo->addItems(*_relevant_teams); // and put in options from a subset ( generated list )
combo->installEventFilter(const_cast<TeamSelectDelegate *>(this));
return combo;
To copy to clipboard, switch view to plain text mode
"_relevant_teams" is a previously generated list of options.
This did not work... it seemed to delete all items in the DB!
Thanks for sharing your ideas.
Cheers!
mike
Bookmarks