Results 1 to 8 of 8

Thread: hoverLeaveEvent ??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default hoverLeaveEvent ??

    Hi

    i have a class that inherits QGraphicsItem that reimplements the hoverLeaveEvent method. Every instance of this class could have several children (that also inherit from QGraphicsItem and reimplement hoverLeaveEvent), that are painted into the item area. Everytime the mouse moves over one of these children, the hoverLeaveEvent of the parent is called, while I don't want this to happen (or I need a way to ignore it). How can I do this?

    P.S.
    every item involved invokes the method setAcceptsHoverEvents(true)

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: hoverLeaveEvent ??

    Try this
    Qt Code:
    1. //In constructor of parent item class
    2. ParentItem::ParentItem()
    3. {
    4. ...
    5. setHandlesChildEvents(false);
    6. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: Sorry misread the post. Have you reimplemented hoverEnterEvent() ?
    Last edited by Gopala Krishna; 16th February 2007 at 15:22. Reason: updated contents
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hoverLeaveEvent ??

    Yes, I reimplemented hoverEnterEvent and hoverLeaveEvent for all the classes involved.

    Calling setHandlesChildEvent() don't give better results...

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

    Default Re: hoverLeaveEvent ??

    Test the cursor position in the event whether it actually is inside the item's shape. Remember about mapping coordinates from screen to item.

  5. #5
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hoverLeaveEvent ??

    Isn't there a more elegant solution?

    My actual implementation looks like this:

    Qt Code:
    1. void ParentClass::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) {
    2.  
    3. qreal x = event->pos().x();
    4. qreal y = event->pos().y();
    5.  
    6. if ( x <= boundingRect().x()
    7. x >= boundingRect().right()
    8. y <= boundingRect().y()
    9. y >= pos.y() >= boundingRect().bottom() ){
    10.  
    11. doSomething();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    An alternative to the preceding should be like this:

    Qt Code:
    1. void ParentClass::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) {
    2. QPointF tmpPos = event->pos();
    3. if ( !boundingRect().contains( tmpPos ) )
    4. doSomething()
    5. }
    To copy to clipboard, switch view to plain text mode 

    but the result isn't the same... anyone can explain me why????
    Last edited by gattogio; 16th February 2007 at 17:15.

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

    Default Re: hoverLeaveEvent ??

    You should test against the shape, not the bounding rectangle.

    How are the results different?

  7. #7
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hoverLeaveEvent ??

    I test the bounding rectangle because my shape is compliant to the bounding rectangle.
    The responses between the two code frames above are different because th e second one doesn't care about the edge of the rectangle.

    I suspect a bug in the Qt Libraries, the method description is:

    bool QRectF::contains ( const QPointF & point ) const
    Returns true if the given point is inside or on the edge of the rectangle; otherwise returns false.

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

    Default Re: hoverLeaveEvent ??

    These are floating point calculations. You can be suffering from approximations.

    I just looked at the sources. The method is correct. It does the same thing as your first method (just normalises the rectangle first).

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
  •  
Qt is a trademark of The Qt Company.