PDA

View Full Version : QTableView selected row not in visible area



mstegehu
21st June 2010, 10:58
Hello,

If, due to logic, I select a row in my table the selected row is not automaticaly scrolled to the visible area of the TableView.



// Get model index based on markerId
QModelIndexList indexList (m_markerProjectionModel->match (m_markerProjectionModel->index (0,0), Qt::DisplayRole, markerId, 1, Qt::MatchExactly));
if (1 == indexList.count ())
{
m_markersProjectionTableView->selectRow (indexList [0].row ());
}


How can I get the selected item always visible in the visible area?

regards,

Marcel

jpujolf
21st June 2010, 11:57
I never tried, but perhaps you can use a call to scrollTo :


// Get model index based on markerId
QModelIndexList indexList (m_markerProjectionModel->match (m_markerProjectionModel->index (0,0), Qt::DisplayRole, markerId, 1, Qt::MatchExactly));
if (1 == indexList.count ())
{
m_markersProjectionTableView->selectRow (indexList [0].row ());
m_markersProjectionTableView->scrollTo( indexList[0]); // <= ADD THIS LINE
}


See HERE (http://doc.qt.nokia.com/4.6/qabstractitemview.html#scrollTo)

waynew
21st June 2010, 22:33
m_markersProjectionTableView->scrollTo( indexList[0]); // <= ADD THIS LINE


I would add to that:


m_markersProjectionTableView->scrollTo( indexList[0], QAbstractItemView::EnsureVisible);

I have similar code and it always scrolls to the selected row.

jpujolf
22nd June 2010, 07:45
If you look at documentation, you can see this :


void QAbstractItemView::scrollTo ( const QModelIndex & index, ScrollHint hint = EnsureVisible ) [pure virtual]

so no real need of 2nd parameter here ( only if you want to use PositionAtTop, PositionAtBottom, PositionAtCenter )