0

I have a number of QGraphicsTextItem and QGraphicsItem painted inside a QGraphicsView. This QGraphicsView has been added to the main Qwidget.

I have written the "FocusOutEvent" for this QGraphicsTextItem and the focus is getting removed only when the "MousePressEvent" is called within the QGraphicsView.

Now my concern here is, How to remove the focus of this QGraphicsTextItem when the MousePressEvent is called Outside the QGraphicsView?

In my MainWindow.cpp, I wrote a mousePressEvent function :

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

But this is not clearing the QGraphicsTextItem.

Expecting a positive response.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Bharathi
  • 337
  • 1
  • 5
  • 17

1 Answers1

1

A QGraphicsTextItem is not a widget, but a QGraphicsItem. Graphics items are added to a QGraphicsScene and viewed by one or more QGraphicsView widgets.

The code presented is only calling clear focus on the currently focussed widget, but since the QGraphicsTextItem is not a widget, it won't be cleared.

In order to clear the focus on the QGraphicsTextItem, call its clearFocus function. If you don't have a pointer to the item, you can get a list of all the items in the scene with the items() function and iterate through them.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • 1
    Thanks a lot for your answer. I used the Items() to get the list of scene items and iterated over them to clear it. It worked fine. – Bharathi Oct 28 '14 at 04:41