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. When Custom QGraphicsPolygonItem dragged, the coordinates of the Polygon don't update
Forum Update on Monday, May 27th 2025

When Custom QGraphicsPolygonItem dragged, the coordinates of the Polygon don't update

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicspolygo
4 Posts 3 Posters 474 Views
  • 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.
  • N Offline
    N Offline
    n0832866
    wrote on last edited by
    #1

    Hi!

    So as the title suggests I am having an issue with the coordinates within a custom QGraphicsPolygonItem I have made.
    Here is the .h file for the custom class:

    #ifndef CUSTOMGPOLYGON_H
    #define CUSTOMGPOLYGON_H
    
    #include <QObject>
    #include <QGraphicsPolygonItem>
    #include <string>
    #include <QGraphicsSceneMouseEvent>
    #include <QMenu>
    #include <QGraphicsTextItem>
    
    class CustomGPolygon : public QObject, public QGraphicsPolygonItem
    {
        Q_OBJECT
    public:
        CustomGPolygon(QPolygonF poly, QObject *parent);
        ~CustomGPolygon();
        using QGraphicsPolygonItem::boundingRect;
        using QGraphicsPolygonItem::paint;
    
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    
    
        QGraphicsTextItem *className;
    
    private slots:
        void deletePolygon();
        void copyPolygon();
    
    
    signals:
        void duplicatePoly(QPolygonF);
    
    private:
        QMenu menu;
    
    };
    
    #endif // CUSTOMGPOLYGON_H
    

    And here is the .cpp:

    #include "customgpolygon.h"
    #include <iostream>
    CustomGPolygon::CustomGPolygon(QPolygonF poly, QObject *parent):QGraphicsPolygonItem(poly)
    {
        menu.addAction("Copy", this, SLOT(copyPolygon()));
        menu.addAction("Delete", this, SLOT(deletePolygon()));
    
        connect(this, SIGNAL(duplicatePoly(QPolygonF)), parent, SLOT(drawPolygon(QPolygonF)));
    }
    
    CustomGPolygon::~CustomGPolygon()
    {}
    
    void CustomGPolygon::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    
        if(event->buttons() & Qt::RightButton)
        {
            menu.exec(event->screenPos());
            this->setSelected(false);
        }
    }
    
    void CustomGPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        if(event->buttons() & Qt::LeftButton)
            {
            QGraphicsItem::mouseMoveEvent(event);
    
    
            QPolygonF poly = this->polygon();
    
    
                std::cout<<poly.at(0).x()<<std::endl;
                std::cout<<poly.at(0).y()<<std::endl;
    
            }
    
    }
    
    void CustomGPolygon::deletePolygon()
    {
        delete this;
    }
    
    void CustomGPolygon::copyPolygon()
    {
        QPolygonF poly = this->polygon();
        emit duplicatePoly(poly);
    }
    

    For the time being I am just trying to std::cout the x and y coordinates of the 1st vector in the QPolygonF, whenever the item is dragged. The shape moves fine, however the console log just displays the same number over and over.

    Thanks for any assistance!

    Pl45m4P 1 Reply Last reply
    0
    • N n0832866

      Hi!

      So as the title suggests I am having an issue with the coordinates within a custom QGraphicsPolygonItem I have made.
      Here is the .h file for the custom class:

      #ifndef CUSTOMGPOLYGON_H
      #define CUSTOMGPOLYGON_H
      
      #include <QObject>
      #include <QGraphicsPolygonItem>
      #include <string>
      #include <QGraphicsSceneMouseEvent>
      #include <QMenu>
      #include <QGraphicsTextItem>
      
      class CustomGPolygon : public QObject, public QGraphicsPolygonItem
      {
          Q_OBJECT
      public:
          CustomGPolygon(QPolygonF poly, QObject *parent);
          ~CustomGPolygon();
          using QGraphicsPolygonItem::boundingRect;
          using QGraphicsPolygonItem::paint;
      
          void mousePressEvent(QGraphicsSceneMouseEvent *event);
          void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
      
      
          QGraphicsTextItem *className;
      
      private slots:
          void deletePolygon();
          void copyPolygon();
      
      
      signals:
          void duplicatePoly(QPolygonF);
      
      private:
          QMenu menu;
      
      };
      
      #endif // CUSTOMGPOLYGON_H
      

      And here is the .cpp:

      #include "customgpolygon.h"
      #include <iostream>
      CustomGPolygon::CustomGPolygon(QPolygonF poly, QObject *parent):QGraphicsPolygonItem(poly)
      {
          menu.addAction("Copy", this, SLOT(copyPolygon()));
          menu.addAction("Delete", this, SLOT(deletePolygon()));
      
          connect(this, SIGNAL(duplicatePoly(QPolygonF)), parent, SLOT(drawPolygon(QPolygonF)));
      }
      
      CustomGPolygon::~CustomGPolygon()
      {}
      
      void CustomGPolygon::mousePressEvent(QGraphicsSceneMouseEvent *event)
      {
      
          if(event->buttons() & Qt::RightButton)
          {
              menu.exec(event->screenPos());
              this->setSelected(false);
          }
      }
      
      void CustomGPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
      {
          if(event->buttons() & Qt::LeftButton)
              {
              QGraphicsItem::mouseMoveEvent(event);
      
      
              QPolygonF poly = this->polygon();
      
      
                  std::cout<<poly.at(0).x()<<std::endl;
                  std::cout<<poly.at(0).y()<<std::endl;
      
              }
      
      }
      
      void CustomGPolygon::deletePolygon()
      {
          delete this;
      }
      
      void CustomGPolygon::copyPolygon()
      {
          QPolygonF poly = this->polygon();
          emit duplicatePoly(poly);
      }
      

      For the time being I am just trying to std::cout the x and y coordinates of the 1st vector in the QPolygonF, whenever the item is dragged. The shape moves fine, however the console log just displays the same number over and over.

      Thanks for any assistance!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @n0832866 said in When Custom QGraphicsPolygonItem dragged, the coordinates of the Polygon don't update:

              QPolygonF poly = this->polygon();
      
      
              std::cout<<poly.at(0).x()<<std::endl;
              std::cout<<poly.at(0).y()<<std::endl;
      

      This wont work (at least not in the case, you expect). In other words: It behaves like it should, but that' s not what you want probably.
      This part of your code returns the polgon coordinates in respect to each other (your polygonItem). So as long as you dont change your polygon, it will always return the same values.

      If you want to see the X / Y coordinates of your PolygonItem on your QGraphicsScene / QGraphicsView, you need to do something like this:

      void CustomGPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
      {
          if(event->buttons() & Qt::LeftButton)
              {
              QGraphicsItem::mouseMoveEvent(event);
      
                  
                  std::cout << pos().x() <<std::endl;
                  std::cout << pos().y() << std::endl;
      
              }
      
      }
      

      EDIT:

      Or you map your inner polygon coordinates to scene (to get an actual point of your polygon inside your polygonItems bounding rect) while moving the item across the scene.
      https://doc.qt.io/archives/qt-4.8/qgraphicsitem.html#mapToScene
      Either as QPolygon(F) or you map every QPoint(F) individually


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      N Please_Help_me_DP 2 Replies Last reply
      2
      • Pl45m4P Pl45m4

        @n0832866 said in When Custom QGraphicsPolygonItem dragged, the coordinates of the Polygon don't update:

                QPolygonF poly = this->polygon();
        
        
                std::cout<<poly.at(0).x()<<std::endl;
                std::cout<<poly.at(0).y()<<std::endl;
        

        This wont work (at least not in the case, you expect). In other words: It behaves like it should, but that' s not what you want probably.
        This part of your code returns the polgon coordinates in respect to each other (your polygonItem). So as long as you dont change your polygon, it will always return the same values.

        If you want to see the X / Y coordinates of your PolygonItem on your QGraphicsScene / QGraphicsView, you need to do something like this:

        void CustomGPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
        {
            if(event->buttons() & Qt::LeftButton)
                {
                QGraphicsItem::mouseMoveEvent(event);
        
                    
                    std::cout << pos().x() <<std::endl;
                    std::cout << pos().y() << std::endl;
        
                }
        
        }
        

        EDIT:

        Or you map your inner polygon coordinates to scene (to get an actual point of your polygon inside your polygonItems bounding rect) while moving the item across the scene.
        https://doc.qt.io/archives/qt-4.8/qgraphicsitem.html#mapToScene
        Either as QPolygon(F) or you map every QPoint(F) individually

        N Offline
        N Offline
        n0832866
        wrote on last edited by
        #3

        @Pl45m4 Thank you very much for your reply! The mapping of the inner coordinates to the scene worked perfectly!

        1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @n0832866 said in When Custom QGraphicsPolygonItem dragged, the coordinates of the Polygon don't update:

                  QPolygonF poly = this->polygon();
          
          
                  std::cout<<poly.at(0).x()<<std::endl;
                  std::cout<<poly.at(0).y()<<std::endl;
          

          This wont work (at least not in the case, you expect). In other words: It behaves like it should, but that' s not what you want probably.
          This part of your code returns the polgon coordinates in respect to each other (your polygonItem). So as long as you dont change your polygon, it will always return the same values.

          If you want to see the X / Y coordinates of your PolygonItem on your QGraphicsScene / QGraphicsView, you need to do something like this:

          void CustomGPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
          {
              if(event->buttons() & Qt::LeftButton)
                  {
                  QGraphicsItem::mouseMoveEvent(event);
          
                      
                      std::cout << pos().x() <<std::endl;
                      std::cout << pos().y() << std::endl;
          
                  }
          
          }
          

          EDIT:

          Or you map your inner polygon coordinates to scene (to get an actual point of your polygon inside your polygonItems bounding rect) while moving the item across the scene.
          https://doc.qt.io/archives/qt-4.8/qgraphicsitem.html#mapToScene
          Either as QPolygon(F) or you map every QPoint(F) individually

          Please_Help_me_DP Offline
          Please_Help_me_DP Offline
          Please_Help_me_D
          wrote on last edited by
          #4

          @Pl45m4 I liked your signature :)

          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