barnabyr
15th April 2006, 03:29
Hi !
I Have a subclassed QScrollArea with a grid layout and it is dynamically filled with lots of thumbnails.
Each thumbnail image is placed into a QFrame which is then added into the grid layout
.. When they are all added I call adjustSize() and show the scrollarea.
I have reimplemented the scroll area's mousePressEvent(QMouseEvent *e) and to query
whether a thumbnail is being clicked my code is like this:
for (int i = 0; i < m_grdLayout->count(); i++)
{
QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
if (layoutItem->geometry().contains(e->pos()))
{
return layoutItem->widget();
}
}
It didn't work and I investigated it ... it seemed that the y coordinate e->pos() was
returning were always the same .. regardless of the size of the scroll area or where I
clicked within it. (up the top or down the bottom)
It was like the postion was relative to the boundary of the containing widget .. not the
scroll area.
I tried using every mapTo and mapFrom I could think of to transform pos of but I could
never get the position in the mouse event to be relative to the place in the scroll area it
was supposed to be.
so I changed my code to :
for (int i = 0; i < m_grdLayout->count(); i++)
{
QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
QWidget * w = layoutItem->widget();
if (w->underMouse())
return w;
}
which works properly.
But why didn't my first try work ? What was I doing wrong ?
thanks for any pointers
barnaby.
I Have a subclassed QScrollArea with a grid layout and it is dynamically filled with lots of thumbnails.
Each thumbnail image is placed into a QFrame which is then added into the grid layout
.. When they are all added I call adjustSize() and show the scrollarea.
I have reimplemented the scroll area's mousePressEvent(QMouseEvent *e) and to query
whether a thumbnail is being clicked my code is like this:
for (int i = 0; i < m_grdLayout->count(); i++)
{
QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
if (layoutItem->geometry().contains(e->pos()))
{
return layoutItem->widget();
}
}
It didn't work and I investigated it ... it seemed that the y coordinate e->pos() was
returning were always the same .. regardless of the size of the scroll area or where I
clicked within it. (up the top or down the bottom)
It was like the postion was relative to the boundary of the containing widget .. not the
scroll area.
I tried using every mapTo and mapFrom I could think of to transform pos of but I could
never get the position in the mouse event to be relative to the place in the scroll area it
was supposed to be.
so I changed my code to :
for (int i = 0; i < m_grdLayout->count(); i++)
{
QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
QWidget * w = layoutItem->widget();
if (w->underMouse())
return w;
}
which works properly.
But why didn't my first try work ? What was I doing wrong ?
thanks for any pointers
barnaby.