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. How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?
Forum Updated to NodeBB v4.3 + New Features

How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.1k 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.
  • T Offline
    T Offline
    tushu
    wrote on last edited by
    #1

    I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted

    bool myViewer::eventFilter(QObject* watched, QEvent* event)
    {
        bool filterEvent = false;
    
        switch (event->type()) 
       {
            case QEvent::MouseButtonPress: 
            {
                 FindingPosition(event);
                 break;
            }
       }
    }
    
    void myViewer::FindingPosition(QEvent* event)
    {
         QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
         QPointF mousePoint = _view->mapToScene(mouseEvent->pos());
         foreach(QGraphicsItem* t , _scene->items())
         {
               if(t->contains(t->mapFromScene(mousePoint)))
               {
                   QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(t);
                   if (rItem) 
                   {
                       rItem->setSelected(false);
                       QPen mPen;
                       mPen.setWidth(1);
                       mPen.setBrush(Qt::red);
                       rItem->setPen(mPen);
                       break;
                     }
                     else
                     {
                         QGraphicsPathItem* pItem = qgraphicsitem_cast<QGraphicsPathItem*>(t);
                         if (pItem) 
                         {
                             pItem->setSelected(false);
                             QPen mPen;
                             mPen.setWidth(1);
                             mPen.setBrush(Qt::red);
                             pItem->setPen(mPen);
                             break;
                         }
                     }
                 }
             }
        }
    }
    
    T JonBJ 2 Replies Last reply
    0
    • T tushu

      I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted

      bool myViewer::eventFilter(QObject* watched, QEvent* event)
      {
          bool filterEvent = false;
      
          switch (event->type()) 
         {
              case QEvent::MouseButtonPress: 
              {
                   FindingPosition(event);
                   break;
              }
         }
      }
      
      void myViewer::FindingPosition(QEvent* event)
      {
           QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
           QPointF mousePoint = _view->mapToScene(mouseEvent->pos());
           foreach(QGraphicsItem* t , _scene->items())
           {
                 if(t->contains(t->mapFromScene(mousePoint)))
                 {
                     QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(t);
                     if (rItem) 
                     {
                         rItem->setSelected(false);
                         QPen mPen;
                         mPen.setWidth(1);
                         mPen.setBrush(Qt::red);
                         rItem->setPen(mPen);
                         break;
                       }
                       else
                       {
                           QGraphicsPathItem* pItem = qgraphicsitem_cast<QGraphicsPathItem*>(t);
                           if (pItem) 
                           {
                               pItem->setSelected(false);
                               QPen mPen;
                               mPen.setWidth(1);
                               mPen.setBrush(Qt::red);
                               pItem->setPen(mPen);
                               break;
                           }
                       }
                   }
               }
          }
      }
      
      T Offline
      T Offline
      tushu
      wrote on last edited by
      #2

      @JonB @SGaist @ChrisW67 @jsulm Can anyone help me ?

      1 Reply Last reply
      0
      • T tushu

        I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted

        bool myViewer::eventFilter(QObject* watched, QEvent* event)
        {
            bool filterEvent = false;
        
            switch (event->type()) 
           {
                case QEvent::MouseButtonPress: 
                {
                     FindingPosition(event);
                     break;
                }
           }
        }
        
        void myViewer::FindingPosition(QEvent* event)
        {
             QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
             QPointF mousePoint = _view->mapToScene(mouseEvent->pos());
             foreach(QGraphicsItem* t , _scene->items())
             {
                   if(t->contains(t->mapFromScene(mousePoint)))
                   {
                       QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(t);
                       if (rItem) 
                       {
                           rItem->setSelected(false);
                           QPen mPen;
                           mPen.setWidth(1);
                           mPen.setBrush(Qt::red);
                           rItem->setPen(mPen);
                           break;
                         }
                         else
                         {
                             QGraphicsPathItem* pItem = qgraphicsitem_cast<QGraphicsPathItem*>(t);
                             if (pItem) 
                             {
                                 pItem->setSelected(false);
                                 QPen mPen;
                                 mPen.setWidth(1);
                                 mPen.setBrush(Qt::red);
                                 pItem->setPen(mPen);
                                 break;
                             }
                         }
                     }
                 }
            }
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @tushu said in How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?:

        foreach(QGraphicsItem* t , _scene->items())

        Qt Graphics is designed so that a much faster way of doing this is QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const (or possibly QList<QGraphicsItem *> QGraphicsScene::items(const QPointF &pos, ...)).

        I know nothing about your polylines or highlighting.

        T 1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          Check the mapping of mouse position to the scene is right or not. You know the exact positions and rects of all items and debug into the code FindingPosition(QEvent* event) and stop at the wrong selection of the item. Manually check the mouse position and item geometry. You will be able to find the issue quickly. Do not look at the code only.

          I had a quick look at what I have. I used customized classes to inherit QGraphicsItem and override
          protected:
          void mousePressEvent( QGraphicsSceneMouseEvent * event ) override;
          In this way it may be easier for you to know which one to highlight.

          1 Reply Last reply
          0
          • JonBJ JonB

            @tushu said in How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?:

            foreach(QGraphicsItem* t , _scene->items())

            Qt Graphics is designed so that a much faster way of doing this is QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const (or possibly QList<QGraphicsItem *> QGraphicsScene::items(const QPointF &pos, ...)).

            I know nothing about your polylines or highlighting.

            T Offline
            T Offline
            tushu
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Did you make the items selectable ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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