2014-06-30 44 views
0

我有一個從QGraphicsScene繼承的類。我在這個類中有一個mouseMoveEvent。基於鼠標移動,我通過信號發送座標到主窗口。我在主窗口中有多個GraphicViews。基於接收信號的場景,我使用QGraphicsTextItem顯示場景座標。 問題是,當我移出場景區域時,我無法隱藏QGraphicsTextItem。 有人可以爲我解決這個問題嗎?檢測鼠標不在QGraphicsScene

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 
} 

回答

1

在場景中安裝事件過濾器。

scene1->installEventFilter(this); 

然後實現方法在由「此」引用的類:

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

剛剛試了一下,它的工作!如果您要在多個對象上安裝事件過濾器,請使用「已觀察」來區分它們。

最好,