get and set current item in QTableView
I can get the current row and column via the index
Code:
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)
Code:
tableViewPowerDegree->setCurrentIndex(...)
seems not to be an option, at least I do not see how to use it.
Matthias
Re: get and set current item in QTableView
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)
Re: get and set current item in QTableView
TableWidget is not an option for several reasons. I am interested in the solution of this very basic usage problem.
Re: get and set current item in QTableView
Use your selection model:
Re: get and set current item in QTableView
Quote:
Originally Posted by
Lykurg
Use your selection model:
With this command I can set an index, which was what I had shown as code already.
Quote:
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.
Re: get and set current item in QTableView
Quote:
Originally Posted by
pospiech
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.
Quote:
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:
Code:
model
()->setItemData
( const QModelIndex & index,
const QMap<int, QVariant>
& roles
)
Quote:
Thus I can not set an index that is not selected anyway.
From the docs:
Quote:
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...
Re: get and set current item in QTableView
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).
Re: get and set current item in QTableView
Quote:
Originally Posted by
pospiech
selectCell(row, column).
=
Code:
tableView
->selectionModel
()->select
(tabelView
->model
()->index
(row,colum
),
QItemSelectionModel::Select);
Re: get and set current item in QTableView
Thank you!
I have now implemented the following to select and edit the next cell in the row.
Code:
QModelIndex index
= tableViewPowerDegree
->currentIndex
();
int row = index.row() + 1;
int column = 1;
QModelIndex newIndex
= tableViewPowerDegree
->model
()->index
(row,column
);
tableViewPowerDegree->setCurrentIndex(newIndex);
tableViewPowerDegree->setFocus();
tableViewPowerDegree->edit(newIndex);