PDA

View Full Version : get and set current item in QTableView



pospiech
20th May 2009, 11:58
I can get the current row and column via the index


QModelIndex index = tableViewPowerDegree->currentIndex();
tableViewPowerDegree->selectRow(index.row() + 1);
tableViewPowerDegree->selectColumn(1);

However I cannot modify the index and setting the row and column one
after the other
as above, because that really selects the comlete row and column, but
not the item at (row, column)


tableViewPowerDegree->setCurrentIndex(...)

seems not to be an option, at least I do not see how to use it.

Matthias

jano_alex_es
20th May 2009, 12:24
you'll need to try with tableWidget instead tableView.

You can access the data trough the index... but this is kinda "cheating", if you want to edit the data inside the table I think you have to use tableWidget (editable but it doesn't have "setModel()", so you need to fill it manually)

pospiech
20th May 2009, 12:27
TableWidget is not an option for several reasons. I am interested in the solution of this very basic usage problem.

Lykurg
20th May 2009, 13:02
Use your selection model:

tableViewPowerDegree->selectionModel()->setCurrentIndex ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command );

pospiech
20th May 2009, 16:46
Use your selection model:

tableViewPowerDegree->selectionModel()->setCurrentIndex ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command );

With this command I can set an index, which was what I had shown as code already.


tableViewPowerDegree->setCurrentIndex(...)


However 'index' does not offer any possibilty to modify the data of row and column.
Thus I can not set an index that is not selected anyway.

Please prove me that I am wrong.

Lykurg
20th May 2009, 21:04
With this command I can set an index, which was what I had shown as code already.

with the difference that you can specify the selection mode.


However 'index' does not offer any possibilty to modify the data of row and column.
The index himself can surely not edit the data (it's not the job of the view), but giving the index to your source model you can modify your data:
model()->setItemData ( const QModelIndex & index, const QMap<int, QVariant> & roles )


Thus I can not set an index that is not selected anyway.
From the docs:

To set an item as the current item without selecting it, call
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);

If that's not what you are looking for, please say again what you really want because then I don't have get you right...

pospiech
20th May 2009, 21:18
I do not want to set data. I only want to select a single cell at a position (row, column) which is not the current selected item. (Sorry, obviously my title was wrong)

I know how to get the index of the current QModelIndex and from that I can get the information about the current row and column. But I can not create a ModelIndex and change its row and column. Therefore a setCurrentIndex is not helping, since its row and column can not be modified.

There exists a selectRow and selectColumn, but I am looking for a selectCell(row, column).

Lykurg
20th May 2009, 22:04
selectCell(row, column).
=

tableView->selectionModel()->select(tabelView->model()->index(row,colum), QItemSelectionModel::Select);

pospiech
25th May 2009, 13:25
Thank you!

I have now implemented the following to select and edit the next cell in the row.



QModelIndex index = tableViewPowerDegree->currentIndex();
int row = index.row() + 1;
int column = 1;
QModelIndex newIndex = tableViewPowerDegree->model()->index(row,column);
tableViewPowerDegree->selectionModel()->select(newIndex, QItemSelectionModel::Select);
tableViewPowerDegree->setCurrentIndex(newIndex);
tableViewPowerDegree->setFocus();
tableViewPowerDegree->edit(newIndex);