Results 1 to 6 of 6

Thread: MouseMoveEvent deal between QGraphicsView and QGraphicsItem

  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default MouseMoveEvent deal between QGraphicsView and QGraphicsItem

    Hi,

    I have very similar problem regarding this subject.

    I have a QGraphicsView and QGraphicsScene and some QGraphicsItem (as usual). For again to zoom upan a rubberband action I needed to reimplement mouse move event in QGraphicsView. Moreover, I also want to change the color of the item whenever mouse moves on the item. So it seemed I need to reimplement the mouse move event also in the QGraphicsItem. But I doesn't work. I also tried out eventFilter thing.

    Can anyone help me?

    Thanks in advance

  2. #2
    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: MouseMoveEvent deal between QGraphicsView and QGraphicsItem

    For changing color of item when mouse is over, you dont need to capture mouse move events.

    You simply need a check in the paint function -
    Qt Code:
    1. if(option->state & QStyle::State_MouseOver)
    To copy to clipboard, switch view to plain text mode 
    and then color the item accordingly.

  3. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: MouseMoveEvent deal between QGraphicsView and QGraphicsItem

    Hi,

    Ok but what if I want to handle the coloring issue in the mouse move event? I see that this is a general problem and asked by some users of this forum before. Is there a way to reimplement mouse move event both in GraphicsView and GraphicsItem?

    Edit: By the way it doesn't work unless I remove option->state part of the if statement. Moreover, although I remove this part, It changes the color that it must change when the program begins, but you know I want to change the color of the item when the mouse is on the item. I could provide my code if you want.

    Thanks in advance
    Last edited by zgulser; 6th April 2009 at 09:11.

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: MouseMoveEvent deal between QGraphicsView and QGraphicsItem

    Hi,

    Does anybody has an idea on this subject?

  5. #5
    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: MouseMoveEvent deal between QGraphicsView and QGraphicsItem

    You will need to store a variable for color.
    Say you have QColor m_color in your graphics item class.
    Then in paint event you do the following -
    Qt Code:
    1. paint()
    2. {
    3. QColor color = m_color;
    4. if(option->state & QStyle::State_MouseOver)
    5. color = m_color.dark();
    6.  
    7.  
    8. painter->fillRect(boundingRect(),color);
    9. }
    To copy to clipboard, switch view to plain text mode 

    thats it, and now when you hover over the item, it will become dark.
    Remember to set QGraphicsItem::setAcceptHoverEvents to true

  6. The following user says thank you to aamer4yu for this useful post:

    Casper14 (22nd July 2014)

  7. #6
    Join Date
    Oct 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MouseMoveEvent deal between QGraphicsView and QGraphicsItem

    Quote Originally Posted by zgulser View Post
    Ok but what if I want to handle the coloring issue in the mouse move event? I see that this is a general problem and asked by some users of this forum before. Is there a way to reimplement mouse move event both in GraphicsView and GraphicsItem?
    You must add QGraphicsView::mouseMoveEvent(event) in your reipmlementation code of mouseMoveEvent. Thus your extended QGraphicsView's mouseMoveEvent is able to act like orginal QGraphicsView's mouseMoveEvent.

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 21:50
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 11:31
  3. Replies: 2
    Last Post: 12th February 2009, 10:53
  4. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 11:56

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.