Note that there are some explanatory texts on larger screens.

plurals
  1. POQSQLTableModel inheritor and QTableView
    primarykey
    data
    text
    <p>I wrote QSQLTableModel inheritor for working with qml and it's work well. I need use it with QTableView too, data shows, but I cannot modify it - when I edit everything is ok, but all changes drop when I get out from field (I know about editStrategy, but the problem occurs earlier). I suppose that something wrong with virtual function, but I cant undestant what. If i create QSqlTableModel with the same parameters, everything is ok. Somebody have any idea how can i fix this? My code:</p> <p>h:</p> <pre><code>class ListModel : public QSqlTableModel { Q_OBJECT Q_PROPERTY( int count READ rowCount() NOTIFY countChanged()) signals: void countChanged(); public: Q_INVOKABLE QVariant data(const QModelIndex &amp;index, int role) const; ListModel(QObject *parent, QSqlDatabase _db):QSqlTableModel(parent,_db){this-&gt;setEditStrategy(QSqlTableModel::OnManualSubmit);} void applyRoles(); #ifdef HAVE_QT5 virtual QHash&lt;int, QByteArray&gt; roleNames() const{return roles;} #endif private: int count; QHash&lt;int, QByteArray&gt; roles; }; </code></pre> <p>cpp:</p> <pre><code>//based on http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML void ListModel::applyRoles() { roles.clear(); qDebug()&lt;&lt;"\n"&lt;&lt;this-&gt;tableName(); for (int i = 0; i &lt; this-&gt;columnCount(); i++) { QString role=this-&gt;headerData(i, Qt::Horizontal).toString(); roles[Qt::UserRole + i + 1] = QVariant(role).toByteArray(); qDebug()&lt;&lt;this-&gt;headerData(i, Qt::Horizontal); } #ifndef HAVE_QT5 setRoleNames(roles); #endif } QVariant ListModel::data(const QModelIndex &amp;index, int role) const{ QVariant value; if(role &lt; Qt::UserRole) { value = QSqlQueryModel::data(index, role); } else { int columnIdx = role - Qt::UserRole - 1; QModelIndex modelIndex = this-&gt;index(index.row(), columnIdx); value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole); } return value; } </code></pre> <p><strong>UPD</strong> I understood that the problem is in <code>data</code> method's quantifier const, if I remove it everything is ok with QTableView, but I cant get data from model with gml's listviews. I see only one solution - replace interition from QSqlTableModel with aggregation it, but maybe someone knows better solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload