PDA

View Full Version : mouse tracking in QGraphicsItem



christina123y
7th March 2009, 11:50
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 :

void Cordinate::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsItem::mouseMoveEvent(event);
float x = event->pos().x;
qDebug() << "mouse move " << endl;
}
void Cordinate::mousePressEvent(QGraphicsSceneMouseEven t * event)
{
QGraphicsItem::mousePressEvent(event);
QPointF point = event->pos();
qDebug()<<"mouse press point is ("<< point.x() <<", "<<point.y()<<")"<<endl;
}
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!

wysota
7th March 2009, 13:28
Could you prepare a minimal compilable example reproducing the problem?

boudie
7th March 2009, 17:49
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;

christina123y
8th March 2009, 05:57
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!

christina123y
8th March 2009, 06:05
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.

olidem
8th March 2009, 14:35
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 :

void Cordinate::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsItem::mouseMoveEvent(event);
float x = event->pos().x;
qDebug() << "mouse move " << endl;
}
void Cordinate::mousePressEvent(QGraphicsSceneMouseEven t * event)
{
QGraphicsItem::mousePressEvent(event);
QPointF point = event->pos();
qDebug()<<"mouse press point is ("<< point.x() <<", "<<point.y()<<")"<<endl;
}
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

christina123y
8th March 2009, 15:05
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 !

aamer4yu
8th March 2009, 17:28
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 :)

christina123y
9th March 2009, 02:01
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?

aamer4yu
9th March 2009, 05:28
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 :)

christina123y
9th March 2009, 09:23
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.