InsertRow in QSqlRelationalTableModel via QPushButton not working correctly
-
Hello all,
unfortunately I can't get on with my program at the moment.
I can already call an insertRow via a QPushButton, insert the data and the whole thing is saved to the database. My Codepart:Part of constructor of MainWindow:
mFungusRecordList->setModel(mFungusFilter); // mFungusRecordList = QListView, mFungusFilter = Subclass QSortFilerProxyModel mFungusRecordList->setModelColumn(Column::Name); ui->redlistEditor->setModel(mFungusDataModel->relationModel(Fungus::Redlist)); //mFungusDataModel = QSqlRelationTableModel and source Model of mFungusFilter ui->redlistEditor->setModelColumn(Column::Name); mFungusMapper->setModel(mFungusFilter); mFungusMapper->setItemDelegate(new FungusSqlRelationalDelegate(this)); mFungusMapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); mFungusMapper->addMapping(ui->idEditor, Fungus::Id, "text"); mFungusMapper->addMapping(ui->headerEditor, Fungus::Fullname, "text"); mFungusMapper->addMapping(ui->fullnameEditor, Fungus::Fullname); mFungusMapper->addMapping(ui->genusEditor, Fungus::Genus); mFungusMapper->addMapping(ui->speciesEditor, Fungus::Species); mFungusMapper->addMapping(ui->familyEditor, Fungus::Family); mFungusMapper->addMapping(ui->orderEditor, Fungus::Order); mFungusMapper->addMapping(ui->commentsEditor, Fungus::Comments); mFungusMapper->addMapping(ui->redlistEditor, Fungus::Redlist); mFungusMapper->addMapping(ui->idView, Fungus::Id, "text"); mFungusMapper->addMapping(ui->headerView, Fungus::Fullname, "text"); mFungusMapper->addMapping(ui->genusView, Fungus::Genus, "text"); mFungusMapper->addMapping(ui->speciesView, Fungus::Species, "text"); mFungusMapper->addMapping(ui->familyView, Fungus::Family, "text"); mFungusMapper->addMapping(ui->orderView, Fungus::Order, "text"); mFungusMapper->addMapping(ui->commentsView, Fungus::Comments, "text"); mFungusMapper->addMapping(ui->redlistView, Fungus::Redlist, "text"); // Connect QLineEdit textChange to QLabel connect(ui->fullnameEditor, &QLineEdit::textChanged, ui->headerEditor, &QLabel::setText); connect(ui->fullnameEditor, &QLineEdit::textChanged, this, &MainWindow::handleSaveButton); connect(ui->genusEditor, &QLineEdit::textChanged, this, &MainWindow::handleSaveButton); connect(ui->speciesEditor, &QLineEdit::textChanged, this, &MainWindow::handleSaveButton); mFungusMapper->toFirst(); mFungusRecordList->setCurrentIndex(mFungusFilter->index(0,1));
My insert new record slot:
void MainWindow::on_newRecordButton_clicked() { int rowCount = mFungusFilter->sourceModel()->rowCount(); if(mFungusFilter->sourceModel()->insertRow(rowCount)){ mFungusMapper->setCurrentIndex(rowCount); mFungusMapper->submit(); ui->idEditor->setText(QString::number(rowCount+1)); ui->stackedWidget->setCurrentIndex(Mode::Editor); } else { qDebug() << "Error insert new row to mFungusFilter"; } }
My save slot:
void MainWindow::on_saveButton_clicked() { if(mFungusMapper->submit()){ mFungusFilter->sort(Column::Name); const int row = mFungusMapper->currentIndex(); const QModelIndex fungusFilterIndex = mFungusFilter->index(row, Column::Name); mFungusMapper->setCurrentIndex(row); mFungusRecordList->setCurrentIndex(fungusFilterIndex); ui->stackedWidget->setCurrentIndex(Mode::View); } else { qDebug() << "Saving error: " << mFungusDataModel->lastError().text(); } }
But this only works if I create a new record directly after Prorgramm start. If I edit any data in the program, save it and then want to create a new record, I have the following problem:
All my edit fields (QLienEdits, QPlainTextEdit etc.) still contain data.
Okay I thought then I "clear" them simply with the following code in the slot on_newRecordButton_clicked
for(int i=0; i<ui->editorLayout->count();i++){ QWidget *widget = ui->editorLayout->itemAt(i)->widget(); if(QLineEdit *lineedit = qobject_cast<QLineEdit*>(widget)) lineedit->clear(); if(QPlainTextEdit *textfield = qobject_cast<QPlainTextEdit*>(widget)) textfield->clear(); if(QComboBox *box = qobject_cast<QComboBox*>(widget)) box->setCurrentIndex(0); }
But when I insert my data and press save I get the following error message via this method
mFungusDataModel->lastError().text();
It saiys "No Fields to update".Can someone please help me and tell me where my error is?
Thank you very much!
-
Hi,
Why are you calling submit twice ?
-
Here's what I found out. If I omit the sort function
mFungusFilter->sort(Column::Name);
of the QSortFilterProxyModel, I can edit and insert data as much as I want.How this is related to the sort function I could not find out yet. Can anyone explain this to me or know why this occurs there?
-
Your mapper is on top of your proxy. When you insert a new row, it will be filtered so your are likely not manipulating the row you think you are.
-
I have figured out my problem. Thanks for your help. Here are the code parts that led to the solution.
void MainWindow::on_newRecordButton_clicked() { const int row = mFungusFilter->sourceModel()->rowCount(); if(mFungusFilter->sourceModel()->insertRow(row)){ const QModelIndex sourceIndex = mFungusFilter->sourceModel()->index(row,1); const QModelIndex index = mFungusFilter->mapFromSource(sourceIndex); mFungusMapper->setCurrentModelIndex(index); for(int i=0; i<ui->editorLayout->count();i++){ QWidget *widget = ui->editorLayout->itemAt(i)->widget(); if(QLineEdit *lineedit = qobject_cast<QLineEdit*>(widget)) lineedit->clear(); if(QPlainTextEdit *textfield = qobject_cast<QPlainTextEdit*>(widget)) textfield->clear(); if(QComboBox *box = qobject_cast<QComboBox*>(widget)) box->setCurrentIndex(0); } ui->idEditor->setText(QString::number(row+1)); ui->stackedWidget->setCurrentIndex(Mode::Editor); } else { qDebug() << "Error insert new row to mFungusFilter"; } }
void MainWindow::on_saveButton_clicked() { if(mFungusMapper->submit() && mGermanFungusNameDataModel->submitAll()){ mFungusFilter->sort(Column::Name); int row = -1; int id = ui->idEditor->text().toInt(); for(int i=0 ;i<mFungusFilter->rowCount();i++){ const QModelIndex index = mFungusFilter->index(i, 0); const QVariant data = index.siblingAtColumn(0).data(); if(data.toInt() == id) { row = index.row(); break; } } const QModelIndex index = mFungusFilter->index(row,1); mFungusMapper->setCurrentIndex(row); mFungusRecordList->setCurrentIndex(index); mFungusRecordList->scrollTo(index); ui->stackedWidget->setCurrentIndex(Mode::View); } else { QString errorMessage = mFungusDataModel->lastError().text(); if(errorMessage.contains("UNIQUE constraint failed")){ const int row = mFungusMapper->currentIndex(); const QModelIndex index = mFungusFilter->index(row,1); mMessageBox->setText(QString("Der Pilz '%1' ist bereits in der Datenbank enthalten.").arg(index.data().toString())); mMessageBox->setStandardButtons(QMessageBox::Ok); mMessageBox->exec(); } else { qDebug() << "Saving error: " << errorMessage; } } }