Extend QSqlTableModel & Update model data;
-
@SGaist
Thanks for your help. If I inherit formQSqlTableModel
and add an extra column which function I have to overwrite, onlydata
,headerData
andsetData
?
Yes of course, if I callsetData
with a specificQModelIndex
the signaldataChanged
knows the index of the updated cell, but my problem is the step before. I receive position updates every second. The update contains an id corresponding the primary key of an database object and an new position. Now I want to update the appropriate row in the model, so I need a mapping from the id to the row. Or it's possible build a model containing the whole object and connect the update signal to the object slot? -
Also columnCount, otherwise your column won't be shown.
AFAIK, you currently have to do that by hand e.g. using a QHash.
-
Thanks for your help.
I decide to inherit fromQSortFilterProxyModel
, extend it to add additional columns and set thesourceModel
toQSqlTableModel
.class ExtraColumnProxyModel : public QSortFilterProxyModel { Q_OBJECT public: ExtraColumnProxyModel(QObject* parent = 0); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant headerData( int section , Qt::Orientation orientation , int role = Qt::DisplayRole ) const; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QModelIndex index( int row , int column , const QModelIndex& parent = QModelIndex() ) const; void appendColumn(QString columnName); QHash<int, QByteArray> roleNames() const; private: QHash<int, QByteArray> m_roles; QVector<QString> headerDataVector = QVector<QString>(); };
-
So it's working like you wanted it ?
-
Yes, the model works correctly. Thanks.
If I want to access the data stored in the extraColumns and I the only thing I get is a identifier-string. I have to create aQHash<QString, QModelIndex>
(update it on model change) to get the correct index and aQMap<QModelIndex, $Data>
to store and access the values. Or is there a cleaner solution? -
Are you moving rows around ?
-
You could put that information at the same index using a custom role but it might be slower to get to the right index when searching for it.
-
An example of
myModel->setData(myCustomValue, Qt::UserRole + 1);
?