// get the item id from the hosting view's model
int objid = index.model()->data(index, Qt::DisplayRole).toInt();
- // find matching object from QComboBox's model
+ // find a matching object from the editor's model
QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
+ // If an object was found in the editor's model create an index to the editor's model
QModelIndex ind = indList.empty() ?
QModelIndex() :
cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
+ // Get the name field of an object using the created index
QString txt = ind.isValid() ?
cbmodel->data(ind).toString() : "";
+ // Render the name text
painter->save();
painter->drawText(option.rect, txt);
painter->restore();
class QSqlQueryModel;
+/*
+* This delegate is for a certain column in a host view in which the model provides
+* an id of a data object. This delegate renders a name of that data object instead of id in the model.
+* In addition this delegate provides a QComboBox editor for selecting an object of featured type in
+* the column in question.
+*/
class ComboBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
private slots:
void commitAndCloseEditor(int);
private:
+ // This model is for the editor data - objects in editor list (this is not a pointer to view's model)
QSqlQueryModel *cbmodel;
int cbmodelIdColumn;
int cbmodelDisplayColumn;