简体   繁体   中英

Qt mouseMoveEvent not firing when mouse pressed

I have a custom QGraphicsScene in which I have a mouseMoveEvent(QGraphicsSceneMouseEvent *event);

When I hover on the scene with the mouse, the mouseMoveEvent gets properly fired.
However when I hover with a mouse button pressed, then it does not get fired anymore. This is how I setup the whole scene in the main window:

scene = new NodeScene(this);  -> My Custom QGraphicsScene class
scene->setSceneRect(QRectF(0, 0, 5000, 5000));

QHBoxLayout *layout = new QHBoxLayout;
view = new QGraphicsView(scene);
layout->addWidget(view);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setMouseTracking(true);

QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
scene->setCentralWidget(widget);

And here is the code where I do handle mouse events (it's for Maya execution):

void NodeScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    MGlobal::displayInfo("Move");
    QGraphicsScene::mouseMoveEvent(event);
}

void NodeScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    MGlobal::displayInfo("Press");
    QGraphicsScene::mousePressEvent(event);
}

void NodeScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    MGlobal::displayInfo("Release");
    QGraphicsScene::mouseReleaseEvent(event);
}

Any idea how I can get the mouseMoveEvents even when a mouse button is pressed ?

It sounds like you're not implementing all of the mouse events, just the mouseMoveEvent.

When overriding a mouse event, you should handle all of them (move, press and release events).

You can then set a boolean in the press event to know whether or not the mouse button is held down when entering the mouseMove event.

Found the issue by comparing my code to other examples, and it is due to the setDragMode(QGraphicsView::RubberBandDrag); line. The code should by default be on QGraphicsView::NoDrag and the RubberBandDrag be enabled only upon press.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM