How to interactively adding text to scene
Hi,
I need to provide a way to add/modify text to scene interactively, just like Windows' Painter program. Are there any sample codes? It seems to be quite complicated...
I can add text using QGraphicsScene::keyPressEvent together with QGraphicsSimpleTextItem, but I would like to make it work exactly the same way as Windows' Painter program....
Help is very appreciated...
Re: How to interactively adding text to scene
use QGraphicsTextItem
I also add the following code to make it editable:
Code:
QVariant MyTextEdit
::itemChange(GraphicsItemChange change,
const QVariant &value
) {
switch (change) {
case ItemSelectedHasChanged:
if (value.toBool()) { // selected
setTextInteractionFlags(Qt::TextEditorInteraction);
} else { // deselected
setTextInteractionFlags(Qt::NoTextInteraction);
}
break;
default:
break;
}
}
Re: How to interactively adding text to scene
Thanks!
However, the character I enter is taken twice, for instance, I enter "t", it shows up as "tt", I can't seem to find the reason. Here is the code:
Code:
{
currentTextItem = addText ( "" );
currentTextItem->setPos( event->scenePos() );
currentTextItem->setTextInteractionFlags( Qt::TextEditorInteraction );
cursor = currentTextItem->textCursor();
}
if ( currentTextItem ) {
cursor.clearSelection();
cursor.insertText( event->text() );
}
}
private:
};
Re: How to interactively adding text to scene
You dont need to handle keypress events. The QGraphicsTextItem class actually embeds QTextEdit into a QGraphicsItem and preserves all its functionality. The only thing you need to do is to change that Qt::TextEditorInteraction flag to make it editable.
Re: How to interactively adding text to scene
Quote:
Originally Posted by
psih128
You dont need to handle keypress events. The QGraphicsTextItem class actually embeds QTextEdit into a QGraphicsItem and preserves all its functionality. The only thing you need to do is to change that Qt::TextEditorInteraction flag to make it editable.
Wow! Thanks!
I spent the entire night digging into the problem, and read QGraphicsTextItem document several times. Not a good documentation in this class....