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

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



  • 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!



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



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



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



  • @Pl45m4 I liked your signature :)


Log in to reply