PDA

View Full Version : Find row that object occupies in grid layout



Ishmael
2nd June 2010, 20:39
Suppose I have multiple widgets in a grid layout. How do I find what row and column a particular object occupies? It's easy to find an object if you know the row and column using itemAt(), but I would like to do the reverse: int row = my_object->isAtRow() or something like that. Thanks for your help!

SixDegrees
2nd June 2010, 20:49
QGridLayout::indexOf(QWidget*) returns an integer index that corresponds to a row-major traversal of grid layout. You can get the row by dividing the index by the width of the grid, and the column by taking the modulus.

Note that this assumes none of your widgets span more than a single cell, although you can still put together a workable scheme if you have detailed knowledge of such things in advance.

See also QGridLayout::getItemPosition() once you have the index.