QGridLayout itemAtPosition with multiple items in a cell
-
Per a suggestion I found on another forum, I implemented grid lines with a custom
QWidget
with apaintEvent
that draws the grid lines for me based on its max rows/cols. I then added it as such:m_layout->addWidget(m_gridLine, 0, 0, -1, -1);
This works great, and grid lines are displayed perfectly. However, my application also depends upon the use of the
itemAtPosition
function within QGridLayout, in my case used to see if a widget is already occupying a space when the user tries to add it there:for (int i = 0; i < data.rowSpan; ++i) { for (int j = 0; j < data.colSpan; ++j) { int row = data.row + i; int col = data.col + j; QLayoutItem *item = m_layout->itemAtPosition(row, col); if (item && item->widget() != m_gridLine) return true; } }
with
data
being a struct containing{row, col, rowSpan, colSpan}
The issue I'm facing, however, is that no matter what,
itemAtPosition
always returns the grid line widget. This is in spite of me callinglower()
on the grid line widget after each widget is added (which is a user-expedited runtime process).Thus, essentially what I'm looking for is a way to "prioritize" certain widgets and "deprioritize" others, so that
QGridLayout::itemAtPosition
will always return the item e.g. at the top. -
What you could do to verify your input and to get what you expect later, is testing your "data" with:
m_layout->getItemPosition(dataIndex, int *row, int *column, int *rowSpan, int *columnSpan)
Therefore you only have to know the layout index of your data item and then you might be able to receive the valid input.
This is in spite of me calling lower() on the grid line widget after each widget is added (which is a user-expedited runtime process).
I dont think this will change something, since all widgets are in the
QGridLayout
and they all have their own cell and index. -