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

QGraphicsSceneMosueEvent is not working.



  • I have this in header file,
    #ifndef CUSTOMVIEW_H
    #define CUSTOMVIEW_H
    #include <QGraphicsView>
    #include <QtWidgets>
    #include <QWidget>
    #include <QtGui>
    #include <QGraphicsItem>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsSceneMouseEvent>
    #include <QDebug>
    class CustomView : public QGraphicsView
    {
    public:
    CustomView(QWidget* parent= 0);
    protected:
    void wheelEvent(QWheelEvent *event);
    protected:
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *e);
    };

    #endif // CUSTOMVIEW_H

    now in cpp file:-
    #include "customview.h"
    CustomView::CustomView(QWidget *parent) : QGraphicsView(parent)
    {

    }
    void CustomView::wheelEvent(QWheelEvent *event)
    {

    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    double scaleFactor = 1.15;
    if(event->delta() >0)
    {
    scale(scaleFactor, scaleFactor);
    }
    else{
    scale(1/scaleFactor, 1/scaleFactor);
    }
    }

    void CustomView::mousePressEvent(QGraphicsSceneMouseEvent *e)
    {
    int x = e->scenePos().x();
    qDebug()<<x;
    }

    ///// But :( its not working.
    I want to know position of mouse on image.
    thanks for help


  • Lifetime Qt Champion

    mousePressEvent(QGraphicsSceneMouseEvent *event) is a method of QGraphicsItem not QGraphicsScene. You can reimplement certain methods (those that are marked virtual in the base class) when creating a subclass but you can't just reimplement them in wild like you are currently doing.

    From what I understand of your knowledge of C++, you should take a look at the basics like inheritance and polymorphism.


  • Lifetime Qt Champion

    Hi,

    @khalistan said:

    void CustomView::mousePressEvent(QGraphicsSceneMouseEvent *e)

    There's no such method in QGraphicsView, it's mouseMoveEvent(QMouseEvent * event)



  • So how can I know mouse position on QGraphicsView. Only for graphics view.
    I tried in MainWindow
    void QGraphicsSceneMouseEvent::mousePressedEvent(*e){
    int x = e.scenePos().x();
    qDebug()x;
    //Now its works but then i got warning "inconsistent dll linkage"
    }
    /I just want to know mouse position on graphics when I press mouse on it/
    Thanks


  • Lifetime Qt Champion

    I already wrote it: the function you have to reimplement in your subclass is mouseMoveEvent(QMouseEvent * event).

    As for the DLL linkage problem, you have an answer on your other thread.



  • But Qt 5.0.1 version provide this method.
    QGraphicsView::mousePressEvent(QGraphicsSceneMouseEvent *event){
    qDebug()<<event->scenePos();
    }
    // This Work but with warning of dll//
    but if i use in header file,
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    and then work on it in cpp
    voind MainWindow::mousePressEvent(QGraphicsSceneMouseEvent *event){
    qDebug()<<event->scenePos();
    }
    //Its show nothing
    I am just new on QT.
    I am web-programmer, so normally working with javascript/php.
    So its completely new for me.
    (I want to learn it, Can you help me.. I will pay you , if you will teach me, not so much but tille bit..My skype id is mand.robin
    )
    Thanks... I will be looking forward for your reply



  • This post is deleted!

  • Lifetime Qt Champion

    mousePressEvent(QGraphicsSceneMouseEvent *event) is a method of QGraphicsItem not QGraphicsScene. You can reimplement certain methods (those that are marked virtual in the base class) when creating a subclass but you can't just reimplement them in wild like you are currently doing.

    From what I understand of your knowledge of C++, you should take a look at the basics like inheritance and polymorphism.



  • @SGaist This solved my problem. I wish that the documentation was clearer, i.e., look in QGraphicsScene for the event.


Log in to reply