Results 1 to 11 of 11

Thread: mouse tracking in QGraphicsItem

  1. #1
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default mouse tracking in QGraphicsItem

    First ,let me explain what problem I have countered.
    I use QGraphicsView Frame.I create a class Cordinate which inherits from QGraphicsItem.This class's main work is to draw a Cordinate System.
    Now I want to track mouse postion in this cordinate system. While I implement mouseMoveEvent(QGraphicsSceneMouseEvent * event), and
    mousePressEvent(QGraphicsSceneMouseEvent * event) in this Cordinate class. And here is my code :
    Qt Code:
    1. void Cordinate::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
    2. {
    3. QGraphicsItem::mouseMoveEvent(event);
    4. float x = event->pos().x;
    5. qDebug() << "mouse move " << endl;
    6. }
    7. void Cordinate::mousePressEvent(QGraphicsSceneMouseEvent * event)
    8. {
    9. QGraphicsItem::mousePressEvent(event);
    10. QPointF point = event->pos();
    11. qDebug()<<"mouse press point is ("<< point.x() <<", "<<point.y()<<")"<<endl;
    12. }
    To copy to clipboard, switch view to plain text mode 
    And when run, the console print out the mouse press point ,but doesn't print out the mouse move?
    Why?Doesn's the item grab the mouse move event? And how I can work out this problem?
    Wish your help, please! Thank you very much!

    Oh, last and the most ,I have another class which is Cordinate class's child item!
    Last edited by wysota; 7th March 2009 at 12:26. Reason: missing [code] tags

  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: mouse tracking in QGraphicsItem

    Could you prepare a minimal compilable example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    christina123y (8th March 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking in QGraphicsItem

    Do you mean that absolutely nothing is output to the console or only no coordinates?
    Because there won't be any coordinates output as long as you don't ask for them.

    qDebug() << "mouse move " << endl;
    should be:
    qDebug() << "mouse move " << point.x() <<", "<<point.y()<<")"<<endl;

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

    christina123y (8th March 2009)

  6. #4
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking in QGraphicsItem

    Quote Originally Posted by wysota View Post
    Could you prepare a minimal compilable example reproducing the problem?
    Thank you for your answer,here is my simple test code .Just in the other computer,it's run correctly.But when copy it to my computer,I don't know why there didn't draw graph ,only draw cordinate.This mean that Cordinate class 's sub item didn't had been executed.Why?And the Cordinate class's mousePressEvent and mouseMoveEvent hasn't been done?
    Where, this code run in the other computer, it can print out mouse press info.but can't print out mouse move info.

    I want to track mouse move position.How can do this success? Wish anyone's help and appreciate very much!
    Attached Files Attached Files
    Last edited by christina123y; 8th March 2009 at 07:06.

  7. #5
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking in QGraphicsItem

    Quote Originally Posted by boudie View Post
    Do you mean that absolutely nothing is output to the console or only no coordinates?
    Because there won't be any coordinates output as long as you don't ask for them.

    qDebug() << "mouse move " << endl;
    should be:
    qDebug() << "mouse move " << point.x() <<", "<<point.y()<<")"<<endl;
    Thank you for your reply. No,I mean I write some qdebug information in mousePressEvent function of Cordinate class. and I also write some qdebug information in mouseMoveEvent funciton of Cordinate class, and Cordinate class which has been called and inherit from QGraphicsItem ,and can draw coordinate system on QGraphicsView. and when i press mouse on the view, the Cordinate::mousePressEvent is called,so it can console the information which i had wrote.
    but the information i had wrote in the Cordinate::mouseMoveEvent function cann't be consoled. I think this function hasn't been called. What i want to is to track mouse move information,such as mouse position in the Cordinate. How can Cordinate::mouseMoveEvent can be called ? Because i think the mouse position information code must be written in this (Cordinate::mouseMoveEvent) function.

  8. #6
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking in QGraphicsItem

    Quote Originally Posted by christina123y View Post
    First ,let me explain what problem I have countered.
    I use QGraphicsView Frame.I create a class Cordinate which inherits from QGraphicsItem.This class's main work is to draw a Cordinate System.
    Now I want to track mouse postion in this cordinate system. While I implement mouseMoveEvent(QGraphicsSceneMouseEvent * event), and
    mousePressEvent(QGraphicsSceneMouseEvent * event) in this Cordinate class. And here is my code :
    Qt Code:
    1. void Cordinate::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
    2. {
    3. QGraphicsItem::mouseMoveEvent(event);
    4. float x = event->pos().x;
    5. qDebug() << "mouse move " << endl;
    6. }
    7. void Cordinate::mousePressEvent(QGraphicsSceneMouseEvent * event)
    8. {
    9. QGraphicsItem::mousePressEvent(event);
    10. QPointF point = event->pos();
    11. qDebug()<<"mouse press point is ("<< point.x() <<", "<<point.y()<<")"<<endl;
    12. }
    To copy to clipboard, switch view to plain text mode 
    And when run, the console print out the mouse press point ,but doesn't print out the mouse move?
    Why?Doesn's the item grab the mouse move event? And how I can work out this problem?
    Wish your help, please! Thank you very much!

    Oh, last and the most ,I have another class which is Cordinate class's child item!
    You should look into the order in which you call

    QGraphicsItem::mousePressEvent(event);

    and your debug code.

    Did you make your Coordniate movable or selectable?

    Try disabling the call to QGraphicsItem::mousePressEvent(event); (and ...Release...). Look what happens then.

    Regards,
    Olli

  9. The following user says thank you to olidem for this useful post:

    christina123y (8th March 2009)

  10. #7
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking in QGraphicsItem

    Quote Originally Posted by olidem View Post
    You should look into the order in which you call

    QGraphicsItem::mousePressEvent(event);

    and your debug code.

    Did you make your Coordniate movable or selectable?

    Try disabling the call to QGraphicsItem::mousePressEvent(event); (and ...Release...). Look what happens then.

    Regards,
    Olli
    Hi, Olli ,thank you very much.I'm so glad to tell you that you are right,when i disabled to call QGraphicsItem::mousePressEvent(event) and QGraphicsItem::mouseMoveEvent(event), both the infomation in those two functions had been printed out.
    Thank you very much!
    Here is another problem,I have seen a thread in this forum ,one said that QGraphicsItem:: mousePressEvent (QGraphicsSceneEvent * event) must be reimplemted if you want to reimplement QGraphicsItem::mouseMoveEvent(QGraphicsSceneEvent * event) , Is this really needed to?
    And from my application,i found that ,only when i press down the mouse, then will execute mousMoveEvent(which means that only when i press the mouse, then the imformation in the mouseMoveEvent function will be printed out.If you don't press mouse ,then can't print out move information ).
    But I want to track mouse positon ,which in fact ,whenever you move the mouse ,then the information in the mouseMoveEvent(QGraphicsSceneEvent * event) must be printed out,
    how can i do this?
    And I had tried to shield the mousePressEvent function, then nothing will be print out in mouseMoveEvent.
    I wonder to know how can i get mousemoveevent without mouse press.
    Wish your reply! Thank you !

  11. #8
    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: mouse tracking in QGraphicsItem

    From the docs..
    void QGraphicsItem::setAcceptHoverEvents ( bool enabled )

    If enabled is true, this item will accept hover events; otherwise, it will ignore them. By default, items do not accept hover events.
    Hope it helps

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

    christina123y (9th March 2009)

  13. #9
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking in QGraphicsItem

    Quote Originally Posted by aamer4yu View Post
    From the docs..


    Hope it helps
    I have already use this: setAcceptHoverEvents(true) in the constuctor function.
    But it still only when you press the mouse ,mouseMoveEvent function then will be executed.
    Is there another solusion?

  14. #10
    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: mouse tracking in QGraphicsItem

    I have already use this: setAcceptHoverEvents(true) in the constuctor function.
    Its setAcceptsHoverEvents not setAcceptHoverEvents ;-)

    that was the prob i cud find the source code u posted,,,, rest still the program is slow.,, will have a look when i get time.. meanwhile u can proceeed with setAcceptsHoverEvents and see u can make it work

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

    christina123y (9th March 2009)

  16. #11
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse tracking in QGraphicsItem

    Quote Originally Posted by aamer4yu View Post
    Its setAcceptsHoverEvents not setAcceptHoverEvents ;-)

    that was the prob i cud find the source code u posted,,,, rest still the program is slow.,, will have a look when i get time.. meanwhile u can proceeed with setAcceptsHoverEvents and see u can make it work
    hi,this problem I have resolved now.I reimplement the mouseMoveEvent(QMouseEvent 8 event) in the class which inherit from QGraphicsView.Then i translate the physical coordinate to logical coordinate .Then the mouse tracking can be shown in the viem.

    Another problem with QGraphicsView Frame. If i want to use QRubberBand to zoom out or zoom in the graphics. and the coordinate change correspondingly. And I get confused.

Similar Threads

  1. mouse tracking on image
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 12th May 2010, 13:06
  2. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 11:19
  3. Replies: 9
    Last Post: 22nd June 2008, 22:26
  4. Replies: 7
    Last Post: 8th September 2006, 16:19
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49

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.