PDA

View Full Version : Edit QGraphicsTextItem when click on it



flavien317
14th June 2014, 14:50
Hello,

I can't edit a QGraphicsTextItem. Only the tab key work, other keys don't work.

So, i create my scene here:


uml::uml(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_pScene = new QGraphicsScene;
ui.graphicsView->setScene(m_pScene);
}

And my QGraphicsTextItem:


QTextEdit* text= widget->findChild<QTextEdit*>("text");
QString strText = methodes->toPlainText();
QGraphicsTextItem* classeText = new QGraphicsTextItem(strText );
classeText ->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable);
classeText ->setTextInteractionFlags(Qt::TextEditorInteraction) ;

With this thing, don't work. How to do for change texte of one of this ?

Benjymack
5th September 2021, 10:43
Was a solution to this found?

I have the same issue.

d_stranz
5th September 2021, 17:08
QGraphicsTextItem uses a QTextDocument to store its content. You can retrieve the document using QGraphicsTextItem::document() and set it using QGraphicsTextItem::setDocument() and can change the contents of that document if your graphics item contains more than just plain text. You can call QGraphicsTextItem::setPlainText() if you just have a string to display.

If you want to interactively edit the content of the QGraphicsTextItem, you need to set the Qt::TextInteractionFlags to Qt::TextEditable using QGraphicsTextItem::setTextInteractionFlags(). The default setting is Qt::NoTextInteraction, which means the item is display-only.

I assume when you set the flag to editable, double-clicking will change the item into a QTextEdit for editing. However, you do not have access to this, only to the QTextDocument. If you need to know what the user has changed the text to, then you can connect to the document's signals.