Edit QGraphicsTextItem inside QGraphicsItemGroup

1.5k views Asked by At

I want a python application that displays a bunch of small portraits and a names below it. Like that: This is how the application looks like

They should be movable and editable (by double clicking the text).

I am using PyQt4 so I figured out, it would be easiest to use a QGraphicsView and a QGraphicsScene for the canvas. So I subclassed a QGraphicsItemGroup like this:

from PyQt4 import QtCore, QtGui
class Speaker(QtGui.QGraphicsItemGroup):
    def __init__(self, name, parent=None):
        QtGui.QGraphicsItemGroup.__init__(self, parent)

        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
        self.text = QtGui.QGraphicsTextItem(name)
        self.text.setTextInteractionFlags(QtCore.Qt.TextEditorInteraction)
        self.addToGroup(self.text)
        self.portrait = QtGui.QGraphicsPixmapItem(QtGui.QPixmap("portrait.png"))
        self.portrait.setY(-35)
        self.addToGroup(self.portrait)

    def keyPressEvent(self, QKeyEvent):
        # Forwarding KeyPress events to the text to enable text editing
        self.text.keyPressEvent(QKeyEvent)

But there are some Problems:

  • Text editing is triggered by a single click, but I want double click (Might be a duplicate of this).
  • You can't use the mouse to select text or move the cursor because the whole group is moved then.
  • If you stop the editing the cursor won't disappear. (Though I know how to do that, if I find a way to activate and deactivate editing mode)

I tried to catch the double click signal and switch to an edit mode that forwards the all the mouse events to the text. But I wasn't able to activate the editing process with the double click and furthermore I couldn't preserve the behavior to end the editing by clicking somewhere else.

So I hope someone can help me. It might be enough to know how to manually activate and deactivate the text interaction mode of the QGraphicsTextItem. Thanks!

1

There are 1 answers

0
bootchk On

Probably you should call QGraphicsItemGroup::setHandlesChildEvents(false).

See the SO question Events with QGraphicsItemGroup