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. QGraphicsSceneMosueEvent is not working.
Qt 6.11 is out! See what's new in the release blog

QGraphicsSceneMosueEvent is not working.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.4k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    khalistan
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #7

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        @khalistan said:

        void CustomView::mousePressEvent(QGraphicsSceneMouseEvent *e)

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • K Offline
          K Offline
          khalistan
          wrote on last edited by
          #3

          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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • K Offline
              K Offline
              khalistan
              wrote on last edited by khalistan
              #5

              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

              1 Reply Last reply
              0
              • K Offline
                K Offline
                khalistan
                wrote on last edited by
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  O 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    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.

                    O Offline
                    O Offline
                    ofmrew
                    wrote on last edited by
                    #8

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved