Results 1 to 9 of 9

Thread: get and set current item in QTableView

  1. #1
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default get and set current item in QTableView

    I can get the current row and column via the index
    Qt Code:
    1. QModelIndex index = tableViewPowerDegree->currentIndex();
    2. tableViewPowerDegree->selectRow(index.row() + 1);
    3. tableViewPowerDegree->selectColumn(1);
    To copy to clipboard, switch view to plain text mode 
    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)
    Qt Code:
    1. tableViewPowerDegree->setCurrentIndex(...)
    To copy to clipboard, switch view to plain text mode 
    seems not to be an option, at least I do not see how to use it.

    Matthias

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default 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)

  3. #3
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: get and set current item in QTableView

    Use your selection model:
    Qt Code:
    1. tableViewPowerDegree->selectionModel()->setCurrentIndex ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: get and set current item in QTableView

    Quote Originally Posted by Lykurg View Post
    Use your selection model:
    Qt Code:
    1. tableViewPowerDegree->selectionModel()->setCurrentIndex ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command );
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: get and set current item in QTableView

    Quote Originally Posted by pospiech View Post
    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:
    Qt Code:
    1. model()->setItemData ( const QModelIndex & index, const QMap<int, QVariant> & roles )
    To copy to clipboard, switch view to plain text mode 

    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...

  7. #7
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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).

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: get and set current item in QTableView

    Quote Originally Posted by pospiech View Post
    selectCell(row, column).
    =
    Qt Code:
    1. tableView->selectionModel()->select(tabelView->model()->index(row,colum), QItemSelectionModel::Select);
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Lykurg for this useful post:

    waynew (14th February 2010)

  10. #9
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. QModelIndex index = tableViewPowerDegree->currentIndex();
    2. int row = index.row() + 1;
    3. int column = 1;
    4. QModelIndex newIndex = tableViewPowerDegree->model()->index(row,column);
    5. tableViewPowerDegree->selectionModel()->select(newIndex, QItemSelectionModel::Select);
    6. tableViewPowerDegree->setCurrentIndex(newIndex);
    7. tableViewPowerDegree->setFocus();
    8. tableViewPowerDegree->edit(newIndex);
    To copy to clipboard, switch view to plain text mode 

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.