PDA

View Full Version : Problem with QGraphicsScene mouse move event



Nesbit
20th April 2012, 13:47
Hello,

I re-implement mouse event handlers in a subclass of QGraphicsScene and have a problem with the mouseMoveEvent() function.

The following code works (i.e. when I press the left/middle/right buttons, hold and move, I have the text displayed):

void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
{
std::cout<<"hehe"<<std::endl;
}

but the following does not (nothing happens even if I press the left button):


void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
{
if(e->button()==Qt::LeftButton) std::cout<<"hehe"<<std::endl;
}

The following code also works

void Scene::mousePressEvent(QGraphicsSceneMouseEvent* e){
if(e->button()==Qt::LeftButton) std::cout<<"hehe"<<std::endl;
}

Hope it's not a bug of Qt and somebody can help.
Thank you so much.

wysota
20th April 2012, 14:39
QGraphicsSceneMouseEvent::button() returns the mouse button that causes the event to happen. Since mouse movement is not related to pressing a mouse button, button() probably returns Qt::NoButton. Try this instead:


if(e->buttons() & Qt::LeftButton) { ... }

Nesbit
22nd April 2012, 18:12
Works like a charm. Thanks, wysota :D