Cannot submit value of a spinbox [SOLVED]
-
Can you show how you setup the mapper ?
-
Here is the constructor of the dialog (frmBeleg):
frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *BelegMod, QWidget *parent) : QDialog(parent), ui(new Ui::frmBeleg) { ui->setupUi(this); mapper=new QDataWidgetMapper(this); mapper->setModel(BelegMod); //mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); mapper->setItemDelegate(new QSqlRelationalDelegate(mapper)); mapper->addMapping(ui->txtBelegNr,2);//<<<<< the spinbox mapper->addMapping(ui->txtAnm1,14);//<<<<<<< the combo if (BelegID != -1) { for (int row = 0; row < BelegMod->rowCount(); ++row) { QSqlRecord record = BelegMod->record(row); if (record.value(0).toInt() == BelegID) {//Spalte des Key-Feldes mapper->setCurrentIndex(row); break; } } } else { mapper->toFirst(); }
-
Addition:
It is no problem to use the passed model from the mainwindow in the constuctor of the dialog (frmBeleg).
When I use the model (BelegMod) in a function, than I have no error at buildung the application but the programm crashes when I use the function in the programm.For example I tried:
void frmBeleg::on_btnSave_clicked() { mapper->submit(); if (BelegMod->submitAll()) BelegMod->database().commit(); else BelegMod->database().rollback(); frmBeleg::close(); }
Or:
void frmBeleg::on_btnNextBelegNr_clicked() { QSqlQueryModel modMaxBelNr; modMaxBelNr.setQuery("SELECT max(BelegNr) FROM Belege"); int nextBelegNr = modMaxBelNr.data(modMaxBelNr.index(0, 0)).toInt(); BelegMod->setData(BelegMod->index(0,2), nextBelegNr+1); }
How can I use the passed model in a function?
Thank's
Franz
-
Are you initializing BelegMod properly in the constructor ?
-
I hope that I have understand your question:
I am initializing the model "modBelege" in the function "on_actionBelege_triggered" like this:
void MainWindow::on_actionBelege_triggered() { ui->stackedWidget->setCurrentIndex(0); modBelege = new QSqlRelationalTableModel(this, cn::db()); modBelege->setEditStrategy(QSqlRelationalTableModel::OnFieldChange); modBelege->setTable("belege"); //modBelege->setRelation(19, QSqlRelation("Zahlarten","ZahlartKey","Zahlart")); modBelege->setRelation(8, QSqlRelation("kontakte","AdrKey","Name")); modBelege->select(); ......
Then I pass the model (modBeleg) to the dialog:
void MainWindow::editBeleg() { //####search the row of the tableview "tabBelege" QModelIndexList indexList = ui->tabBelege->selectionModel()->selectedIndexes(); int row; foreach (QModelIndex index, indexList) { row = index.row(); } //####get Key-value in column 0 int _selBelegKey = ui->tabBelege->model()->data(ui->tabBelege->model()->index(row,0)).toInt(); //#### open dialog: frmBeleg newBeleg(_selBelegKey,modBelege, this); newBeleg.exec(); }
Hope it helps!
-
In the frmBeleg constructor, do you assign modBelege to BelegMod ?
-
From your code it seems that you don't initialize your class member that is also named BeleMod
-
frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *BelegMod, QWidget *parent) : QDialog(parent), ui(new Ui::frmBeleg), BelegMod(BelegMod) {
But yes,
frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *model, QWidget *parent) : QDialog(parent), ui(new Ui::frmBeleg), BelegMod(model) {
is clearer.
One thing I recommend: use a clear naming scheme e.g. camel case with starting letter lowercased for variables and camel case for classes. It will make your code easier to read (following Qt's pattern is good idea)