October 21, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

QModelIndex invalid when returning from a dialog

 

Hi all,
in my project I’ve got a table view and I want to edit some rows using a qdialog subclass. In the dialog I’ve placed a qdatawidgetmapper to map wigets, and the submit policy is manual.
Now when the user clicks the save button on the dialog something like the following happens:

  1. qDebug() << "Current model row index is " << widgetMapper->currentIndex();
  2. tableView->selectRow( widgetMapper->currentIndex() );
  3.         qDebug() << "Selected row " << tableView->currentIndex().row();
  4.         qDebug() << "Index is valid " << tableView->currentIndex().isValid();

where tableview is an instance of a QTableView, widgetmapper is an instance of the QDataWidgetMapper.
Now the outputs presents the following:

  1. Current model row index is  3
  2. Selected row  -1
  3. Index is valid  false

So I’m editing the row number 3 of the table model, I want to select again such row when the dialog hides but suddenly the model index becomes invalid. Of course the widget mapper and the table view share the same QSqlRelationTableModel instance.
Can anybody give me an hint?

1 reply

October 21, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

I think I get it: the problem seems to be that the qdatawidgetmapper::submit() method invalidates (?) the currentIndex or something alike.
In fact the following piece of code works fine:

  1. int modelIndex = widgetMapper->currentIndex();
  2.         retVal = widgetMapper->submit();
  3.         // emphasize the current record
  4.         tableView->selectRow( modelIndex );

while the following is not working:

  1. retVal = widgetMapper->submit();
  2.         // emphasize the current record
  3.        ableView->selectRow( widgetMapper->currentIndex() );

The difference is that in the working code the current index of the qdatawidgetmapper is hold before the actual submit, so I guess the submit produces something on the widget mapper or the model itself that prevents the selection of such index. It is still not clear to me what really happens, can anybody explain better where the problem is?

 
  ‹‹ QTableWidget and Qt Designer      [SOLVED] Combobox menu question ››

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