Sid411
Sid411

Reputation: 703

Detect when mouse is not in QGraphicsScene

I am having a class inherited from QGraphicsScene. I have a mouseMoveEvent in this class. Based on the mouse move I am sending the co ordinates via signal to the main window. I have multiple GraphicViews in main window. Based on the scene from which the signal is received I am display the scene co ordinates using QGraphicsTextItem. The problem is when I move out of the scene area I am unable to hide the QGraphicsTextItem. Can someone give me a work around for this?

 Class Scene::QGraphicsScene
 {
    void MouseMoveEvent(QGraphicsSceneMouseEvent *Event)
    {
       int XPos = event.x();
       int YPos = event.y();
       emit SignalPos(XPos,YPos);
    }
 }

 //In Main Window
 connect(scene1,SignalPos(int,int),this,SlotPos1(int,int);
 //Similarly for scene2,scene3,scene4

 void MainWindow::SlotPos(int X, int Y)
 { 
     m_qgtxtItemX.setText(QString::x);   
    //I want to hide this once I am out of scene.It is a member variable. I tried
    //taking local variable but it didn't work.
    //Similarly for y and other slots
 }

Upvotes: 0

Views: 725

Answers (1)

Omid Sakhi
Omid Sakhi

Reputation: 140

Install an event filter on the scene.

scene1->installEventFilter(this);

then implement the method in the class that is referenced by "this":

bool eventFilter(QObject *watched, QEvent *event) {
    if (event->type() == QEvent::Leave)
    {
        qDebug() << "Mouse left the scene";
    }       
    return false;
}

Just tried it and it worked! If you are installing the event filter on more than one object, please use "watched" to differentiate between them.

Best,

Upvotes: 1

Related Questions