QDataWidgetMapper autosubmit doesn't work
I have created a sqldatabase and populate it with some test data. I'm using a QDataWidgetMapper and I think I've connected right because I can read the test records with this code:
databaseManager = new DatabaseManager();
dataMapper = new QDataWidgetMapper();
dataMapp开发者_StackOverflower->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
dataMapper->setModel(databaseManager->getTableModel());
// aggiungo una riga nella tabella ui per ogni riga nel database
for(int i=0; i<databaseManager->getTableModel()->rowCount(); i++){
this->addRow();
}
dataMapper->toFirst();
And here's the addRow() method (create rows in a QTableWidget and map them to the model):
int Widget::addRow(){
int rowsNum = ui->moneyTableWidget->rowCount();
ui->moneyTableWidget->insertRow(rowsNum);
QLineEdit *newQLineEdit = new QLineEdit();
QLineEdit *newItemLabel = new QLineEdit();
QDoubleValidator *doubleValidator = new QDoubleValidator(0.0, 5.0, 2, this);
newQLineEdit->setValidator(doubleValidator);
QObject::connect(newQLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(updateTotal()));
ui->moneyTableWidget->setCellWidget(rowsNum, 0, newItemLabel);
ui->moneyTableWidget->setCellWidget(rowsNum, 1, newQLineEdit);
dataMapper->addMapping(newQLineEdit,databaseManager->getTableModel()->fieldIndex("price"), "text");
dataMapper->addMapping(newItemLabel,databaseManager->getTableModel()->fieldIndex("item"), "text");
return rowsNum;
}
the problem is that autosubmit doesn't work. If I add rows to the QtableWidget (with the same addRow method) or if I modify/delete existing rows (the ones with test data) nothing happens.
Or better the gui reflects the changes, but re-runnig the application I've got the same test date that I had before.
Any idea? Is there a way to test what is happening to the database? If it were an usual query q I would do a q.lastError().
thanks
When you're setting submit policy for QDataWidgetMapper it defines the way how data is going to get transfered from the widget to the corresponding field of your model. AutoSubmit will transfer data to the model whenever widget looses focus. You should be able to track down changes to your model if would define a handler to the model's dataChanged signal.
Now from your description it looks like you're expecting a new row to be present in the model when you're restarting your app. In order to do this data model should commit the changes ether via manual (QSqlTableModel::submitAll) call or automatically on field or row change. In order to set up the edit strategy for your model use QSqlTableModel::setEditStrategy with one of the possible values:
QSqlTableModel::OnFieldChange - All changes to the model will be applied immediately to the database.
QSqlTableModel::OnRowChange - Changes to a row will be applied when the user selects a different row.
QSqlTableModel::OnManualSubmit - All changes will be cached in the model until either submitAll() or revertAll() is called.
hope this helps, regards
精彩评论