Subclassing widget to take coordinates



  • Hello,

    I have created a widget consisted of a GraphicsView (named graphicsView), on which I display a png image. I want to press the mouse on the graphicsView and take the pixel's coordinates. To achieve this I have subclassed my widget as it can be seen in the following code. But, now I don't know what to use to take the coordinates in two different labels (named label_value_x and label_value_y). I have tried to use QPoint as it seems from the code, but I get errors. I hope someone could help me as I really cannot find any solution.
    Thanks

    ////////// header file //////////
    @class graphicsView : public QGraphicsScene
    {
    public:
    graphicsView( QObject *parent = 0);

    protected:
    void mousePressEvent ( QGraphicsSceneMouseEvent * event );
    };@

    ///////// cpp file ////////////
    @graphicsView::graphicsView( QObject *parent ) :
    QGraphicsScene( parent )
    {
    }

    void graphicsView::mousePressEvent( QGraphicsSceneMouseEvent * event )
    {
    QPoint pos = mapToScene(event->pos());
    double x = pos.x();
    double y = pos.y();

    //graphicsView.mousePressEvent(event.pos());

    label_value_x->setNum(x);
    label_value_y->setNum(y);
    }@



  • Widgets are based on QPoint whereas QGraphicsView/Scene use QPointF everywhere which is just a floating point version of QPoint. Try converting event->pos() to a QPointF before you pass it to mapToScene().



  • Now i have tried this:
    @void graphicsView::mousePressEvent( QGraphicsSceneMouseEvent * event )
    {
    QPointF pos = event->pos();
    double x = pos.x();
    double y = pos.y();

    int k=8;

    label_value_x->setNum(x);
    label_value_y->setNum(y);
    }@

    But when I out a breakpoint in place od (int k=8;), It seems that I don't even have access inside routine void graphicsView::mousePressEvent( QGraphicsSceneMouseEvent * event )
    What is more. it doesn't identify tha labels (label_value_x and label_value_y), instead of been declared. Do you know where is the problem?



  • I think you should be overriding the function:

    @
    void QGraphicsView::mouseMoveEvent ( QMouseEvent * event )
    @

    or

    @
    void QGraphicsScene::mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    @

    you are confusing the scene and view event handlers.



  • When I mahe this change the .exe file stops working when I prees over the image, when I have already opened it. Is there any good example to get coordinates of an image using QGraphicsView?

    ////// .h //////////
    @class graphicsView : public QGraphicsScene
    {
    public:
    graphicsView( QObject *parent = 0);

    protected:
    void mouseMoveEvent ( QMouseEvent * event );
    };@

    ///////// .cpp ///////////
    @graphicsView::graphicsView( QObject *parent ) :
    QGraphicsScene( parent )
    {
    }

    void QGraphicsView::mouseMoveEvent ( QMouseEvent * event )
    {
    QPointF pos = event->pos();
    double x = pos.x();
    double y = pos.y();

    int k=3;

    QLabel *label_value_x;
     QLabel *label_value_y;
    

    label_value_x->setNum(x);
    label_value_y->setNum(y);
    }@


Log in to reply
 

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