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. QGraphicsItem is not selectable after selecting one other QGraphics Item
Qt 6.11 is out! See what's new in the release blog

QGraphicsItem is not selectable after selecting one other QGraphics Item

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 878 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.
  • C Offline
    C Offline
    canrollas
    wrote on last edited by canrollas
    #1

    Hi, I am currently trying to implementing a node editor for myself. But I have a semantic error that I couldnt figure it out. In my code there is a graphicsview and when adding item to the view everythin is fine. After selecting one item retrieves problem that I can not detect other nodes.

    Can anyonde help me about it?

    This is how I add nodes to view.

    WhatsApp Image 2022-11-24 at 11.42.15.jpeg

    This is Nodes added to the application.

    9ed059dd-9fce-4af7-b8e4-0be6b42c9257-image.png

    View Class Code

    void NodeClass::mousePressEvent(QMouseEvent *event)
    {
        if(event->button()==Qt::RightButton){
    
    
            QMenu menu(this);
            menu.addAction("Add Node");
            menu.addAction("Add Node");
    
    
    
            QPointF scenePoint = mapToScene(event->pos());
            menu.exec(event->globalPos());
            Node *myNode = new Node(scene,"Undefined Title");
            myNode->setPos(scenePoint);
    
            //QGraphicsRectItem *button1 = new QGraphicsRectItem(scenePoint.x(),scenePoint.y(), 100, 200);
            //blockList->append(*myNode);
            scene->addItem(myNode);
            //qInfo()<<"This is items scene"<<scene->items();
        }else{
            QGraphicsView::mousePressEvent(event);
    // this is for avoiding the double clicks
        }
    
    }
    
    

    This is code of the mouse event of QGraphicsItem

    void Node::mousePressEvent(QGraphicsSceneMouseEvent *event){
        int xpos = this->pos().x();
        int ypos = this->pos().y();
    
       
    
        int mapped_xpos =  mapToScene(event->pos()).x() ;
        int mapped_ypos =  mapToScene(event->pos()).y() ;
    
    
    
    
    
        if(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+height && event->button()==Qt::LeftButton){
            // Printing the position of the mouse click
            qDebug() << "Mouse Clicked at: " << mapped_xpos << " " << mapped_ypos;
            // Printing the position of the node
            qDebug() << "Node Position: " << xpos << " " << ypos;
            // Printing the position of the node
            qDebug() << "Node Size: " << width << " " << height;
            // Printing the position of the node
            qDebug() << "Node Edge Size: " << edge_size;
            // Printing the position of the node
    
            qDebug() << "Node has choosen!";
            // change the cursor to a holding hand
    
        }else{
    
        }
        
        
    
    }
    
    

    This is console output

    Mouse Clicked at:  371   296
    Node Position:  255   120
    Node Size:  340   480
    Node Edge Size:  35
    Node has choosen!
    
    // But after clicking another node it has the same focus on the QGraphicsItem lastly clicked.
    

    Thanks for your helps.

    A 1 Reply Last reply
    0
    • C canrollas

      Hi, I am currently trying to implementing a node editor for myself. But I have a semantic error that I couldnt figure it out. In my code there is a graphicsview and when adding item to the view everythin is fine. After selecting one item retrieves problem that I can not detect other nodes.

      Can anyonde help me about it?

      This is how I add nodes to view.

      WhatsApp Image 2022-11-24 at 11.42.15.jpeg

      This is Nodes added to the application.

      9ed059dd-9fce-4af7-b8e4-0be6b42c9257-image.png

      View Class Code

      void NodeClass::mousePressEvent(QMouseEvent *event)
      {
          if(event->button()==Qt::RightButton){
      
      
              QMenu menu(this);
              menu.addAction("Add Node");
              menu.addAction("Add Node");
      
      
      
              QPointF scenePoint = mapToScene(event->pos());
              menu.exec(event->globalPos());
              Node *myNode = new Node(scene,"Undefined Title");
              myNode->setPos(scenePoint);
      
              //QGraphicsRectItem *button1 = new QGraphicsRectItem(scenePoint.x(),scenePoint.y(), 100, 200);
              //blockList->append(*myNode);
              scene->addItem(myNode);
              //qInfo()<<"This is items scene"<<scene->items();
          }else{
              QGraphicsView::mousePressEvent(event);
      // this is for avoiding the double clicks
          }
      
      }
      
      

      This is code of the mouse event of QGraphicsItem

      void Node::mousePressEvent(QGraphicsSceneMouseEvent *event){
          int xpos = this->pos().x();
          int ypos = this->pos().y();
      
         
      
          int mapped_xpos =  mapToScene(event->pos()).x() ;
          int mapped_ypos =  mapToScene(event->pos()).y() ;
      
      
      
      
      
          if(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+height && event->button()==Qt::LeftButton){
              // Printing the position of the mouse click
              qDebug() << "Mouse Clicked at: " << mapped_xpos << " " << mapped_ypos;
              // Printing the position of the node
              qDebug() << "Node Position: " << xpos << " " << ypos;
              // Printing the position of the node
              qDebug() << "Node Size: " << width << " " << height;
              // Printing the position of the node
              qDebug() << "Node Edge Size: " << edge_size;
              // Printing the position of the node
      
              qDebug() << "Node has choosen!";
              // change the cursor to a holding hand
      
          }else{
      
          }
          
          
      
      }
      
      

      This is console output

      Mouse Clicked at:  371   296
      Node Position:  255   120
      Node Size:  340   480
      Node Edge Size:  35
      Node has choosen!
      
      // But after clicking another node it has the same focus on the QGraphicsItem lastly clicked.
      

      Thanks for your helps.

      A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      @canrollas
      I'm not sure what you mean by "selectable". You don't seem to use QGraphicItem's standard facility for that (using QGraphicsItem::ItemIsSelectable flag)

      C 1 Reply Last reply
      0
      • A Asperamanca

        @canrollas
        I'm not sure what you mean by "selectable". You don't seem to use QGraphicItem's standard facility for that (using QGraphicsItem::ItemIsSelectable flag)

        C Offline
        C Offline
        canrollas
        wrote on last edited by
        #3

        @Asperamanca said in QGraphicsItem is not selectable after selecting one other QGraphics Item:

        QGraphicsItem::ItemIsSelectable flag

        Hi sir, When I mean selectable => When mouse clicks on it. After click I can detect mouse click and positions. BUt I can not detect other clicks in other Items. ( And thankyou for remind me Selectable Flag ) Do you have opinion about that?

        A 1 Reply Last reply
        0
        • C canrollas

          @Asperamanca said in QGraphicsItem is not selectable after selecting one other QGraphics Item:

          QGraphicsItem::ItemIsSelectable flag

          Hi sir, When I mean selectable => When mouse clicks on it. After click I can detect mouse click and positions. BUt I can not detect other clicks in other Items. ( And thankyou for remind me Selectable Flag ) Do you have opinion about that?

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @canrollas
          First, the event has a scenePos, so you don't need to map the coordinates yourself.
          Second, why

          if(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+height
          

          If the mouse is not on the item, you simply won't get an event anyway

          C 1 Reply Last reply
          1
          • A Asperamanca

            @canrollas
            First, the event has a scenePos, so you don't need to map the coordinates yourself.
            Second, why

            if(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+height
            

            If the mouse is not on the item, you simply won't get an event anyway

            C Offline
            C Offline
            canrollas
            wrote on last edited by
            #5

            @Asperamanca thanks for your warning it works that way but Your answer does not solve my problem.

            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