PDA

View Full Version : determining visible rows/columns of a QTableView/QTreeView



mule
4th February 2012, 17:22
How can the visible rows/columns of a QTableView (and QTreeView) be determined?
More specifically, how can a QModelIndex be created for the currently visible top-left and bottom-right items?

Spitfire
7th February 2012, 09:27
I'm not sure what you mean by 'top-left' and 'bottom-right' items.
Could you elaborate?

Meantime take a look at QItemSelectionModel (http://developer.qt.nokia.com/doc/qt-4.8/QItemSelectionModel.html).

mule
7th February 2012, 15:15
Perhaps an example will demonstrate ...

There is a QTableView that is presenting data like a spreadsheet.
The data are 10 columns (A-J) by 100 rows (1-100).
At any time, only some columns (variable width) and some rows can be scrolled into view.

My question is:
How can I determine what data items are currently viewable?
If I know what data items are being viewed in the top-left corner and bottom-right corner,
then I can know all of the data items that are in view.

Row, Column for each corner would be great.
A ModelIndex for each corner would be great.
But, I cannot figure out how to interrogate the QTableView for any helpful corner information.


Also:
QItemSelectionModel does not help, because I am not dealing with selected items, just items that are viewable.

Spitfire
7th February 2012, 15:44
Ok, I get it now.

That's actually very easy:

MainWindow::MainWindow(QWidget *parent)
:
QMainWindow(parent),
table( new QTableWidget() )
{
this->table->setRowCount( 100 );
this->table->setColumnCount( 10 );

this->setCentralWidget( this->table );

QToolBar* tb = this->addToolBar( "Test" );
tb->addAction( "Test", this, SLOT( test() ) );
}

void MainWindow::test( void )
{
qDebug() << this->table->rowAt( 0 ) << "-" << this->table->rowAt( this->table->height() ); // this is what you want
qDebug() << this->table->columnAt( 0 ) << "-" << this->table->columnAt( this->table->width() ); // this is what you want
}
Edit:
You can also use indexAt( table->rect.topLeft() ) and indexAt( table->rect().bottomRight() ) if index is more convenient for you.

mule
8th February 2012, 13:16
Yes! That is it!
Both (rowAt,columnAt and indexAt) will be extremely helpful.

Thank you.