2014-10-27 65 views
0

我有一個QGraphicsTextItemQGraphicsItem的一個QGraphicsView的內部。這QGraphicsView已被添加到主Qwidget如何從主QWidget中刪除一個QGraphicsTextItem的焦點

我已經爲此QGraphicsTextItem寫了「FocusOutEvent」,並且只有在QGraphicsView中調用「MousePressEvent」時纔會刪除焦點。

現在我關心的是,當MousePressEvent被稱爲QGraphicsView之外時,如何消除此QGraphicsTextItem的焦點?

在我MainWindow.cpp,我寫了一個mousePressEvent功能:

void EyGuiMainWindow::mousePressEvent(QMouseEvent *e) 
{ 
    QWidget *w = QApplication::focusWidget(); 
    if(w) 
    w->clearFocus(); 
} 

但這不清除QGraphicsTextItem

期待積極的迴應。

回答

1

QGraphicsTextItem不是一個小部件,而是一個QGraphicsItem。圖形項目被添加到一個QGraphicsScene並由一個或多個QGraphicsView小部件查看。

提供的代碼只調用焦點集中在當前焦點的小部件,但由於QGraphicsTextItem不是一個小部件,它不會被清除。

爲了清楚關注QGraphicsTextItem,請調用它的clearFocus函數。如果您沒有指向該項目的指針,您可以使用items()函數獲取場景中所有項目的列表,並遍歷它們。

+1

非常感謝您的回答。我使用Items()來獲取場景項目列表並迭代它們以清除它。它運行良好。 – Bharathi 2014-10-28 04:41:05