MyClass::onAddNewItem()
{
model->setTable("mytable");
//This selects no records which is a lot faster, imho, then selecting without a filter.
//Especially if mytable has a lot of rows.
model->setFilter("myid is null"); //fyi, myid is column 0 in the model.
model->select();
//now I know I could use QSqlTableModel::insertRecord() ,
//but I would like to use QAbstractItemModel methods only.
//If using QSqlTableModel specific methods is the answer to my problem,
//or even just a better way of doing it then please let me know.
QAbstractItemModel *abModel
= model;
//Yes I know this is unnecessary but it helps me adhere to my point above.
int newRow = abModel->rowCount();
abModel->insertRow(newRow);
//Column 0 is the auto id, so I don't set it here.
newIndex = abModel->index(newRow, 1);
abModel->setData(newIndex, "myvalue");
}
MyClass::onSaveItem()
{
//Calling abModel->submit() does nothing because of the OnManualSubmit strategy.
//So I created a ModelHandler class that let's me call the correct submit method.
//I'm using this ModelHandler to do other things as well and it looks odd in this example, i know.
myAbModelHandler->submit(); //The end result of this is the same as calling model->submitAll();
//This prints "FALSE".
qDebug() << "New Index validity:" << newIndex.isValid();
//And because of that this statement doesn't work...
qDebug() << "My new row's autoincrement value is: " << newIndex.sibling(newIndex.row(), 0).data().toString();
}
MyClass::onCancelItem()
{
//same as the submit thing above except for revert.
myAbModelHandler->revert(); //The end result of this is the same as calling model->revertAll();
}
MyClass::onAddNewItem()
{
QSqlTableModel *model = new QSqlTableModel(this);
model->setTable("mytable");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
//This selects no records which is a lot faster, imho, then selecting without a filter.
//Especially if mytable has a lot of rows.
model->setFilter("myid is null"); //fyi, myid is column 0 in the model.
model->select();
//now I know I could use QSqlTableModel::insertRecord() ,
//but I would like to use QAbstractItemModel methods only.
//If using QSqlTableModel specific methods is the answer to my problem,
//or even just a better way of doing it then please let me know.
QAbstractItemModel *abModel = model; //Yes I know this is unnecessary but it helps me adhere to my point above.
int newRow = abModel->rowCount();
abModel->insertRow(newRow);
//Column 0 is the auto id, so I don't set it here.
newIndex = abModel->index(newRow, 1);
abModel->setData(newIndex, "myvalue");
}
MyClass::onSaveItem()
{
//Calling abModel->submit() does nothing because of the OnManualSubmit strategy.
//So I created a ModelHandler class that let's me call the correct submit method.
//I'm using this ModelHandler to do other things as well and it looks odd in this example, i know.
myAbModelHandler->submit(); //The end result of this is the same as calling model->submitAll();
//This prints "FALSE".
qDebug() << "New Index validity:" << newIndex.isValid();
//And because of that this statement doesn't work...
qDebug() << "My new row's autoincrement value is: " << newIndex.sibling(newIndex.row(), 0).data().toString();
}
MyClass::onCancelItem()
{
//same as the submit thing above except for revert.
myAbModelHandler->revert(); //The end result of this is the same as calling model->revertAll();
}
To copy to clipboard, switch view to plain text mode
Bookmarks