selectedIndexes() generates an error!
When I try to use the selectedIndexes() method of the QListview to retrieve the QModelIndexList of selected items, this error is generated: ‘virtual QModelIndexList QListView::selectedIndexes() const’ is protected
I addition, I want retrieve the one only selected item index.
- QModelIndexList selected = listView->selectionModel()->selectedIndexes();
protected methods are for internal use only.
To edit a selected record in a tableModel, why this code don’t work!
- QModelIndexList i = ui->listView->selectionModel()->selectedIndexes(); // retrieve selected item indexes
- QModelIndex ii = i.at(0); // I need the one only selected item index
- QModelIndex iii = proxyModel->mapToSource(ii); // corresponding index in the tableModel
- QSqlRecord r = tableModel->record(iii.row()); // return the needed record to edit
- r.setValue(1 ,textToPutInTheField1); // edit the record
First of all, try to use more meaningful variable names. It is a bad idea to use i, ii, iii. It really isn’t that much work to type a meaningful name, and it will make understanding your code by others and by yourself later on much easier.
Then, the question is, what does not work exactly? Do you get an error? What was the result you expected, and what happened instead?
It seems to me that you are trying to modify a value. This is not the way to do it. You get a QSqlRecord, but that record is just a value object. If you change values in it, these are not magically written to the underlying database, nor even to the QSqlTableModel itself. If you want to change the data in a QSqlTableModel, I suggest you use the setData() method. That would also get rid of much of the rest of your code, as chances are, your translation back to the source model index are not needed.
but to edit a record in the tableModel, the concerning record index should be retrieved first using his coresponding proxyModel index. Without the mapToSource() method how can I do this.
You simply do the edit directly on the proxyModel. The proxyModel’s implementation will then do the edit on the corresponding item in the sourceModel (which may also be a proxy model itself, etc.).
Can you define “doesn’t work”?
the concerning record is not edited in the database nor in the tableModel.
I already explained the reasons why that happens in my posting above.
I don’t know who to use setData() in this case, so I conserve the mapToSource() because I find working with records in the tableModel is more intuitive than indexes in the proxyModel.
With this code the record is not edited, but a new record is created with the same information except the field 1 = “anything”
- QModelIndexList indexList = ui->termListView->selectionModel()->selectedIndexes();
- QModelIndex proxyIndex = indexList.at(0);
- QModelIndex tableIndex = proxyModel->mapToSource(proxyIndex);
- QSqlRecord r = tableModel->record(tableIndex.row());
- r.setValue(1 ,"anything");
- tableModel->insertRecord(tableIndex.row(), r);
what I do now?
insertRecord does what it’s name says, it inserts a record, not edits one.
from the docs: [doc.qt.nokia.com]
Inserts the record after row. If row is negative, the record will be appended to the end. Calls insertRows() and setRecord() internally.
Returns true if the row could be inserted, otherwise false.
Perhaps you were looking for: setRecord [doc.qt.nokia.com] ?