Change QGraphicsTextItem edit trigger from single click to double click



  • I am working on a patch for the QtCharts module (https://codereview.qt-project.org/#/c/239957) which main goal is making chart labels (QGrapcshiTextItems) editable.

    I can easily enable the editing of the QGraphicsTextItem with the QGraphicsTextItem::setTextInteractionFlags, but by default a single click will enable the edit mode.

    The reviewers asked me to change this behavior to double click.

    I have tried to ignore the event in the reimplemented mousePressEvent, but the single click still triggers the editing.

    Any other idea would be welcome!


  • Lifetime Qt Champion

    Hi,

    What about using mouseDoubleClickEvent ?



  • @SGaist

    Hi Sam,

    Thank you for your hint, but my first problem was preventing the edit trigger on single click.

    For the posterity I have ended up with the following solution:

    • Disable the textInteraction flags by default
    • Override the QGraphicsTextItem::sceneEvent with the following:
    bool ValueAxisLabel::sceneEvent(QEvent *event)
    {
        if (event->type() == QEvent::GraphicsSceneMouseDoubleClick) {
            setTextInteractionFlags(Qt::TextEditorInteraction);
    
            bool ret = QGraphicsTextItem::sceneEvent(event);
            // QGraphicsTextItem::sceneevent needs to be processed before
            // the focus
            setFocus(Qt::MouseFocusReason);
            return ret;
        }
        return QGraphicsTextItem::sceneEvent(event);
    }
    

    The textInteractionFlags should be disabled in the overridden focusOutEvent.