December 13, 2011

freecamellia freecamellia
Lab Rat
57 posts

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.

9 replies

December 13, 2011

Volker Volker
Robot Herder
5428 posts

Use this:

  1. QModelIndexList selected = listView->selectionModel()->selectedIndexes();

protected methods are for internal use only.

December 13, 2011

freecamellia freecamellia
Lab Rat
57 posts

To edit a selected record in a tableModel, why this code don’t work!

  1. QModelIndexList i = ui->listView->selectionModel()->selectedIndexes(); // retrieve selected item indexes
  2. QModelIndex ii = i.at(0);  // I need the one only selected item index
  3. QModelIndex iii = proxyModel->mapToSource(ii); // corresponding index in the tableModel
  4. QSqlRecord r = tableModel->record(iii.row()); // return the needed record to edit
  5. r.setValue(1 ,textToPutInTheField1); // edit the record

December 13, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 13, 2011

Volker Volker
Robot Herder
5428 posts

Can you define “doesn’t work”?

December 13, 2011

freecamellia freecamellia
Lab Rat
57 posts

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.

December 13, 2011

freecamellia freecamellia
Lab Rat
57 posts
Volker wrote:
Can you define “doesn’t work”?

the concerning record is not edited in the database nor in the tableModel.

December 13, 2011

Andre Andre
Area 51 Engineer
6031 posts

freecamellia wrote:
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.).

freecamellia wrote:
Volker wrote:
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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 13, 2011

freecamellia freecamellia
Lab Rat
57 posts

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”

  1. QModelIndexList indexList = ui->termListView->selectionModel()->selectedIndexes();
  2. QModelIndex proxyIndex = indexList.at(0);
  3. QModelIndex tableIndex = proxyModel->mapToSource(proxyIndex);
  4. QSqlRecord r = tableModel->record(tableIndex.row());
  5. r.setValue(1 ,"anything");
  6. tableModel->insertRecord(tableIndex.row(), r);

what I do now?

December 14, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

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] ?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ Any tutorial for using .ui file      [Solved] "Unavailable synchronous data" when setting variable in object. ››

You must log in to post a reply. Not a member yet? Register here!