PDA

View Full Version : Enable editing by double clicking QGraphicsTextItem



Seishin
6th November 2012, 19:40
Hi,
I'm want to enable the text editing mode of QGraphicsTextItem when double clicking the item.
I tried to directly set the setTextInteractionFlags(Qt::TextEditorInteraction) , but this enables editing mode by single clicking.

I also tried to override the mouseDoubleClickEvent event of QGraphicsTextItem, as the code below. However the text in item is not editable immediately after double clicking. I have to click the text once more to edit the text.


void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event){
setTextInteractionFlags(Qt::TextEditorInteraction) ;

QGraphicsTextItem::mouseDoubleClickEvent(event);
}
void focusOutEvent(QFocusEvent * event){
setTextInteractionFlags(Qt::NoTextInteraction);
QGraphicsTextItem::focusOutEvent(event);
}

Is there a better way to enable editing by double clicking?
Any suggestions are welcome.
Thanks.

----------Update----------
Just found an example in Qt emamples called "Diagramscene". In the file diagramtextitem.cpp, they use almost the same method as mine and cause the same problem.
Futhermore, if you double click the text and then out of focus directly, the text is still keep selected and is able to edit after single click it. Is this a feature or bug?

mhennings
11th December 2012, 15:07
Have a look at my answer in a similar thread, it should contain what you're searching for:

Re: QGraphicsTextItem and text cursor position via QPoint (http://www.qtcentre.org/threads/20332-QGraphicsTextItem-and-text-cursor-position-via-QPoint?p=234213#post234213)