How to get the mouse click coordinate in a scene?



  • Hi..
    I make some circle in one scene using QGraphicsEllipseItem. I want to grab a real coordinate (not the center of the circle) of this item when someone left-clicked every circle. Using the first code it can only detect the coordinate when I clicked outside of the circle but when I clicked inside of every circle, the coordinate not shown.

    @
    void DialogApplication::mousePressEvent(QMouseEvent *event)
    {
    if (event->button()==Qt::LeftButton){
    mouse_koordinat=event->pos();
    qDebug() << "coordinate in my item: " << mouse_koordinat.x();
    }
    }@

    Using this second code there is an error message: "overloaded member function not found in 'DialogApplication' "

    @void DialogApplication::mousePressEvent(QGraphicsSceneMouseEvent *event) //QMouseEvent *event) //
    {
    if (event->button()==Qt::LeftButton){

        mouse_koordinat=event->buttonDownScenePos(Qt::LeftButton);
    
        qDebug() << "coordinate in my item: " << mouse_koordinat.x();
    }
    

    }
    @

    I appreciate if there is a suggestion and help. Thank you...



  • Hi,

    is DialogApplication inherited from QGraphicsView?

    The second error means you MUST define this method in class Declaration



  • Try this:
    @
    #include <QGraphicsEllipseItem>
    #include <QDebug>
    class Ellipse: public QGraphicsEllipseItem
    {
    public:
    Ellipse(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = 0) :
    QGraphicsEllipseItem(x, y, width, height, parent) {}
    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    if (event->button() & Qt::LeftButton)
    {
    QPointF center = rect().center();
    qDebug() << "Center:" << center;
    }
    }
    };
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.