How to setData in foreign key table of QSqlRelationalTableModel via QSortFilerProxyModel?
Unsolved
General and Desktop
-
The following situation exists:
An SQLite database with an N:M relationship. The whole thing is solved via a table betweenTable: Deutscher_Pilzname
+----------------------------------+ | Id | Pilz_id | Deutscher_name_id | +----------------------------------+ | 1 | 700 | 1 | | 2 | 33 | 2 | | .. | ... | ... | +----------------------------------+
Table: Deutscher_Name
+----+----------------+ | Id | Deutscher_Name | +----+----------------+ | 1 | Name_1 | | 2 | Name_2 | | .. | ... | +----+----------------+
Now I'm getting the data out via a QSqlRelationTableModel.
QSqlRelationalTableModel *DatabaseManager::germanFungusNameDataModel() const { QSqlRelationalTableModel *model = new QSqlRelationalTableModel; model->setTable("Deutscher_Pilzname"); model->setRelation(model->fieldIndex("Deutscher_name_id"), QSqlRelation("Deutscher_Name", "Id", "Name")); model->setJoinMode(QSqlRelationalTableModel::LeftJoin); model->setSort(2, Qt::AscendingOrder); // model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit); model->select(); while (model->canFetchMore()) { model->fetchMore(); } return model; }
In the constructor of my mainwindow.cpp I create a subclassed QSortFilerProxyModel and set the source Model and set it to a QListView.
//QSqlRelationTableModel mGermanFungusNameDataModel = mDatabaseManager->germanFungusNameDataModel(); //Subclassed QSortFilerProxyModel mGermanNameDataFilter->setSourceModel(mGermanFungusNameDataModel); mGermanNameDataFilter->sort(Column::GermanName); mGermanNameDataFilter->setSortCaseSensitivity(Qt::CaseInsensitive); //QListView mGermanNameEditor->setModel(mGermanNameDataFilter); mGermanNameEditor->setModelColumn(Column::GermanName);
Now I have created a QDialog where I cann change the text and after that I call this:
const bool germanNameIsSelected = mGermanNameEditor->selectionModel()->hasSelection(); QModelIndex index = mGermanNameEditor->currentIndex(); QModelIndex sourceIndex = mGermanNameDataFilter->mapToSource(index); GermanNameEditor editor(this); if(germanNameIsSelected){ QString name = index.data().toString(); editor.setCurrentText(name); editor.setModal(true); const int type = editor.exec(); if(type == QDialog::Accepted){ QString changedName = editor.currentText(); auto value = sourceIndex.siblingAtColumn(2).data().toString(); while(mGermanFungusNameDataModel->relationModel(2)->canFetchMore()){ mGermanFungusNameDataModel->relationModel(2)->fetchMore(); } qDebug() << "RowCount relation: " << mGermanFungusNameDataModel->relationModel(2)->rowCount(); for(int i=0 ;i<mGermanFungusNameDataModel->relationModel(2)->rowCount();i++){ const QModelIndex index = mGermanFungusNameDataModel->relationModel(2)->index(i, 1); const QVariant data = index.siblingAtColumn(1).data(); if(data.toString() == value) { qDebug() << "setData relationModel: " << mGermanFungusNameDataModel->relationModel(2)->setData(index, changedName, Qt::EditRole); mGermanFungusNameDataModel->database().transaction(); qDebug() << "Submitall: " << mGermanFungusNameDataModel->submitAll(); mGermanFungusNameDataModel->database().commit(); break; } }
setData returns true and submitAll as well, but the whole thing is not changed. Neither in the GUI nor in the database. This is probably related to my subclassed QSortFilterProxyModel. I thought when the sourceModel is changed the QSortFilerProxyModel is updated automatically.
Does anyone have any idea?