Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting textItem from a the QGraphicsView.



  • The behaviour i want is when a user clicks on the screen if there isn't already a QGraphicsTextItem there, the user will add one there, but if there already is, allow for it to be edited.

    if (!drawnLayout->checkTextExists(finalX,finalY)) {
                        QString readableBit = "Enter text: ";
                        std::shared_ptr<Text> text(new Text(*canvasChosen, finalX, finalY, readableBit));
                        drawnLayout->addText(text);
                    }
                    else
                    {
                        foreach(QGraphicsItem *item, items())
                        {
                            if (item->x() == finalX)
                            {
                                if (item->y()==finalY){
                                    item->setTextInteractionFlags(Qt::TextEditorInteraction);
                                    item ->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable);
                                    item->setFocus();
                                }
                            }
                        }
                    }
    

    The logic at the if statement is right I think, but I'm stuck on how to do the part in else, because the 'item' needs to be a QGraphicsTextItem to be able for me to edit and save.


  • Qt Champions 2019

    @jkwok678 said in Getting textItem from a the QGraphicsView.:

    I'm stuck on how to do the part in else, because the 'item' needs to be a QGraphicsTextItem to be able for me to edit and save

    QGraphicsTextItem textItem = qobject_cast<QGraphicsTextItem*>(item);
    if (textItem) {
        // textItem is a QGraphicsTextItem
    }
    

  • Lifetime Qt Champion

    Hi
    Is qgraphicsitem_cast not to be used if one uses the user type ID ? / custom class.
    https://doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast


Log in to reply