Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting textItem from a the QGraphicsView.
Qt 6.11 is out! See what's new in the release blog

Getting textItem from a the QGraphicsView.

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 324 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkwok678
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • J jkwok678

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mrjjM 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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
        }
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved