0

I am using Qt 5.7 (the latest version). I can't get the mouse events to work in QGraphicsScene, but they work in window outside of my scene. I have followed this question.

So I have overwritten QWidget::mouseMoveEvent() in my main widget's subclass like this:

// header:
class MyWidget {
    ...
    void mouseMoveEvent( QMouseEvent * event );
};

// source:
MyWidget::MyWidget() {
    setMouseTracking();
}

void MyWidget::mouseMoveEvent( QMouseEvent * event ) {

}

It doesn't work for: mouseMoveEvent, mouseGrabber, mousePressEvent, mouseReleaseEvent, or mouseDoubleClickEvent. But somehow it only works for mousePressEvent.

Could this be a bug in Qt?

SOURCE CODE: In objectloader.cpp

ObjectLoader::ObjectLoader(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ObjectLoader)
{
  ui->setupUi(this);
   scene=new QGraphicsScene(this);
   ui->graphicsView->setScene(scene);
   ui->graphicsView->setMouseTracking(true);
  setMouseTracking(true);



}

Thats were i set mouse tracking twice In objectloader.h Then i define that method in objectloader.h

class ObjectLoader : public QMainWindow
{
    Q_OBJECT


    public:
        explicit ObjectLoader(QWidget *parent = 0);
        ~ObjectLoader();

    private slots:
    void mouseMoveEvent(QMouseEvent *event);
    protected:

    private:

    };

    #endif // OBJECTLOADER_H

And implementation of that method in objectloader.cpp

void ObjectLoader::mouseMoveEvent(QMouseEvent *event){

    qDebug()<<"Mouse moved";

}
Community
  • 1
  • 1

2 Answers2

3

When a mouse event is generated by Qt it is generally passed initially to the QWidget that was under the mouse pointer when the event was generated. If that QWidget accepts the event then no further processing will take place. If the event isn't accepted then Qt may propogate the event to that QWidget's parent and so on.

In your particular case the mouse move events you are interested in are being sent to the QGraphicsView/QGraphicsScene conponents where they are being accepted and, hence, no further processing takes place. In a case like that you generally need to install an event filter to intercept and process the events of interest.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
G.M.
  • 12,232
  • 2
  • 15
  • 18
1

Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with QWidget::setMouseTracking().

So, I think you should check whether mouseTracking is really enabled or not, by using `bool hasMouseTracking() const'.

hyun
  • 2,135
  • 2
  • 18
  • 20
  • I am sure that it is enabled.When i run this code if(hasMouseTracking()){qDebug()<<"true";} It returns true – Nemanja Milosevic Aug 22 '16 at 07:46
  • Could you post your source? – hyun Aug 22 '16 at 08:48
  • Update mouse events dont work on QtGraphicsView,but work on any other part of window? – Nemanja Milosevic Aug 22 '16 at 11:10
  • `QMainWindow` has a [centralWidget()](http://doc.qt.io/qt-4.8/qmainwindow.html#centralWidget). You should call `setMouseTracking()` for the `centralWidget` as well. So, in your `ObjectLoader` ctor, `centralWidget()->setMouseTracking(true)`. – hyun Aug 22 '16 at 11:35
  • Found out that QGraphicsScene does not accept MouseEvents but QGraphicsSceneMouseEvents,making implementation of that method did not help.Somehow like my GraphicsScene is black spot in window – Nemanja Milosevic Aug 22 '16 at 11:45
  • The mouse move events are being 'consumed' by the `QGraphicsView` and/or the `QGraphicsScene`. If you want to intercept and act on them them I think you'll have to install an [event filter](http://doc.qt.io/qt-5/qobject.html#eventFilter). – G.M. Aug 22 '16 at 11:53
  • @G.M. Do you want to post detailed answer with links and how to implement event filter,in order to help someone in future? – Nemanja Milosevic Aug 22 '16 at 12:17