Results 1 to 7 of 7

Thread: QGraphicsItem hover event

  1. #1
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsItem hover event

    Hi. I'm subclassing a QGraphicsItem, but I can't recieve the hover events. I'm forgoting anything?

    The code is below

    Qt Code:
    1. #include "square.h"
    2.  
    3. #include <QtGui>
    4.  
    5. Square::Square(QPolygonF &polygon, uint row, uint col){
    6. m_polygon=polygon;
    7. m_row=row; m_col=col;
    8. m_hover=false;
    9. setAcceptsHoverEvents(true);
    10. }
    11.  
    12. QRectF Square::boundingRect() const{
    13. return m_polygon.boundingRect();
    14. }
    15.  
    16. void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*){
    17. if(m_hover) painter->setBrush(Qt::red);
    18. painter->drawPolygon(m_polygon);
    19. painter->drawText(boundingRect(), QString("%1x%2").arg(m_row).arg(m_col));
    20. }
    21.  
    22. void Square::hoverEnterEvent(QGraphicsSceneHoverEvent*){
    23. m_hover=true;
    24. }
    25.  
    26. void Square::hoverLeaveEvent(QGraphicsSceneHoverEvent*){
    27. m_hover=false;
    28. }
    To copy to clipboard, switch view to plain text mode 

    Thanks, bye

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

    Default Re: QGraphicsItem hover event

    Are you sure you are not receiving them? Maybe you should call QGraphicsItem::update() to schedule a repaint of the item?
    J-P Nurmi

  3. #3
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem hover event

    Yes, I have test it using QMessageBox::information... And don't works, but if I use mousePressEvent it works

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem hover event

    I think u just need to check if the item has mouse over it...

    u can do it by the following

    Qt Code:
    1. void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
    2. {
    3. if(option->state & QStyle::State_MouseOver) painter->setBrush(Qt::red);
    4. painter->drawPolygon(m_polygon);
    5. painter->drawText(boundingRect(), QString("%1x%2").arg(m_row).arg(m_col));
    6. }
    To copy to clipboard, switch view to plain text mode 

    if(option->state & QStyle::State_MouseOver)

    hope it helps :0

  5. #5
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem hover event

    Don't work

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem hover event

    can we see the changed code u wrote ??
    are u still using m_hover to check the hover event ?? and have u set setAcceptsHoverEvents(true); ???

  7. #7
    Join Date
    Jul 2014
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem hover event

    Quote Originally Posted by aamer4yu View Post
    can we see the changed code u wrote ??
    are u still using m_hover to check the hover event ?? and have u set setAcceptsHoverEvents(true); ???
    This solved it for me.

Similar Threads

  1. QDockWidget-title
    By moowy in forum Qt Programming
    Replies: 18
    Last Post: 23rd April 2014, 20:13
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  3. event problem
    By windkracht8 in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2006, 11:52
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  5. What's the relationship between signal/slot and event?
    By twosnowman in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 17:13

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.