Results 1 to 8 of 8

Thread: mouseEvent in QScrollArea giving wrong position ?

  1. #1
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question mouseEvent in QScrollArea giving wrong position ?

    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:

    Qt Code:
    1. for (int i = 0; i < m_grdLayout->count(); i++)
    2. {
    3. QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
    4. if (layoutItem->geometry().contains(e->pos()))
    5. {
    6. return layoutItem->widget();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    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 :
    Qt Code:
    1. for (int i = 0; i < m_grdLayout->count(); i++)
    2. {
    3. QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
    4. QWidget * w = layoutItem->widget();
    5. if (w->underMouse())
    6. return w;
    7. }
    To copy to clipboard, switch view to plain text mode 
    which works properly.

    But why didn't my first try work ? What was I doing wrong ?

    thanks for any pointers

    barnaby.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    Can we see the whole code of the event handler?

  3. #3
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    constructor ...

    Qt Code:
    1. // Thumbnail View Constructor ..
    2. RoboThumbnailView::RoboThumbnailView(QWidget * parent) : QScrollArea(parent)
    3. {
    4. m_bDragging = false;
    5. m_wgtViewport = new QWidget();
    6. m_grdLayout = new QGridLayout();
    7. m_grdLayout->setSpacing(5);
    8. m_wgtViewport->setLayout(m_grdLayout);
    9. setWidget(m_wgtViewport);
    10. }
    To copy to clipboard, switch view to plain text mode 

    mousePressEvent :

    Qt Code:
    1. void RoboThumbnailView::mousePressEvent(QMouseEvent *e)
    2. {
    3. RoboThumbnailClickable * thumb = thumbAt(e->pos());
    4. if (thumb)
    5. {
    6. if (e->button() == Qt::RightButton)
    7. {
    8. // do context menu.
    9. }
    10. else if (e->button() == Qt::LeftButton || e->button() == Qt::MidButton)
    11. {
    12. m_bDragging = true;
    13. if (e->modifiers() & Qt::ControlModifier)
    14. {
    15. thumb->toggleSelected();
    16. }
    17. else
    18. {
    19. for (int i = 0; i < m_grdLayout->count(); i++)
    20. {
    21. QLayoutItem * child = m_grdLayout->itemAt(i);
    22. static_cast<RoboThumbnailClickable *>(child->widget())->setSelected(false);
    23. }
    24. thumb->setSelected(true);
    25. }
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    thumbAt function that doesn't work ...

    Qt Code:
    1. RoboThumbnailClickable * RoboThumbnailView::thumbAt(const QPoint & pos)
    2. {
    3. for (int i = 0; i < m_grdLayout->count(); i++)
    4. {
    5. QLayoutItem *layoutItem = m_grdLayout->itemAt(i);
    6. if (layoutItem->geometry().contains(pos()))
    7. {
    8. return static_cast<RoboThumbnailClickable *>(layoutItem->widget());
    9. }
    10. }
    11.  
    12. return NULL;
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    Qt Code:
    1. if (e->button() == Qt::RightButton)
    2. {
    3. // do context menu.
    4. }
    To copy to clipboard, switch view to plain text mode 

    There is a specialised event generated for context menus (contextMenuEvent). It's better to handle it that way.

    About your problem now...

    Try this:
    Qt Code:
    1. RoboThumbnailClickable * thumb = thumbAt(e->pos()+QPoint(horizontalScrollBar()->value(), verticalScrollBar()->value()));
    To copy to clipboard, switch view to plain text mode 

    instead of
    Qt Code:
    1. RoboThumbnailClickable * thumb = thumbAt(e->pos());
    To copy to clipboard, switch view to plain text mode 

    and see if it changes anything.

  5. The following user says thank you to wysota for this useful post:

    barnabyr (19th April 2006)

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    You can get the "actual" position by mapping the event's position (which is relative to the scroll area) from the widget's (inside the scroll area) point of view.

    Qt Code:
    1. QPoint pos = widget()->mapFromParent(e->pos());
    2. RoboThumbnailClickable * thumb = thumbAt(pos);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    barnabyr (19th April 2006)

  8. #6
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    They both work ..

    Although the mapFromParent solution makes no sense to me ...

    "From parent" would imply that .. the position in e->pos() is in parent
    coordinates which was what I was finding to be true. ( ie. the y coordinate was
    relative to the parent window not the scroll area) BUT .. the event handler
    is part of my QScrollArea .. not my parent window, so why wasn't
    e->pos() passed as the correct position in the first place ?

    And Wysota thanks for the reminder about contextMenuEvent . I guess I assumed
    it wouldn't be available ...

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    Quote Originally Posted by barnabyr
    Although the mapFromParent solution makes no sense to me ...

    "From parent" would imply that .. the position in e->pos() is in parent
    coordinates which was what I was finding to be true. ( ie. the y coordinate was
    relative to the parent window not the scroll area) BUT .. the event handler
    is part of my QScrollArea .. not my parent window, so why wasn't
    e->pos() passed as the correct position in the first place ?
    "the parent" is probably the QScrollArea widget (the widget which handles the event), and you want its viewport, thus you have to map from the scroll area widget to its "canvas" coordinates. That's exactly the same what my solution does (seems that Trolls use the scrollbar version in their examples, at least the one I've seen -- namely the pie view example).

    And Wysota thanks for the reminder about contextMenuEvent . I guess I assumed it wouldn't be available ...
    Just remember that context menus can be handled in three different ways in Qt4. Choose the one which suits you best.

  10. #8
    Join Date
    Jul 2009
    Location
    China,Shanghai
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouseEvent in QScrollArea giving wrong position ?

    Please use mapToXXX or mapFromXXX to solve the problem.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.