PDA

View Full Version : How to append the next Record into QTableView without removing the previous record?



innobleday
15th March 2010, 04:09
Hi All,

I have a problem when I'm coding. I aim to add/append another record to my QTableView without removing previous record showed. The code below used to be like that. Have you any solution for me?

Thanks a lot.



model->setTable("Users");
model->setSort(User_Id, Qt::AscendingOrder);
model->setHeaderData(User_Name,Qt::Horizontal,tr("Username"));
model->setHeaderData(User_FirstName, Qt::Horizontal, tr("First name"));
model->setHeaderData(User_LastName, Qt::Horizontal, tr("Last Name"));
model->setRelation(User_Mode,QSqlRelation("UserModes","ID","Name"));
model->setHeaderData(User_Mode, Qt::Horizontal, tr("Type"));
model->select();

ChrisW67
15th March 2010, 05:42
Nothing in the code you have posted even hints that you have tried to add a record. Have you tried anything that failed? Have you read the documentation? Looked at the Qt bundled examples?

Start with the QSqlTableModel and QAbstractItemModel class pages and the Model/View Programming Overview in the Qt reference documentation

innobleday
15th March 2010, 06:47
Yes I've looked it. But can't still work.

It seems like this:


QSqlRecord record = modelTableAction->record(0);
record.setValue(Action_Panel,QVariant(tr(panel)));
record.setValue(Action_SwitchName,QVariant(tr(swit chName)));
record.setValue(Action_StateName,QVariant(tr(state Name)));

modelTableAction->setRecord(i,record);

modelTableAction->insertRecord(0,record);
modelTableAction->submitAll();

ChrisW67
26th March 2010, 04:32
Ah, so the problem is that the current index/selection of the views(s) is lost.

The moment you call submitAll() on the QSqlTableModel all the attached views lose their selection and current index (in the docs). Neither QModelIndex nor QPersistentModelIndex is of any use to identify a row through the submitAll().

In the OnManualSubmit edit strategy, if you insert the row using QAbstractItemModel::insertRows() and then set the fields using setData() without calling submitAll() the selection is not affected. If you only add or edit records then this is the approach for you. Deleting records is problematic, because the "deleted" row still exists for this and any other view using it (its headerData() becomes "!").

Another approach is to:

Get the currentIndex() before the submitAll()
Fetch a unique identifier from the record containing that index (preferably in a single column)
Execute the QSqlTableModel::submitAll()
Use QAbstractItemModel::match() to find your unique key
Restore the selection using the index returned by match()

This approach fails if:

The records do not have a unique identifier
The current row is new and the identifier is automatically allocated by the RDBMS when the row is written (that doesn't happen until the submitAll() is called)

I also haven't found the right place for other views to catch this event and preserve their own selections.

For the OnFieldChange and OnRowChange edit strategies the submit comes immediately and selections are lost. You can capture the selection in slots connected to the model's beforeInsert(), beforeUpdate() and beforeDelete() signals but I have yet to find somewhere I can restore an adjusted selection.

I'm all ears for an elegant solution.