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. QGraphicsView mouse move event not work to me

QGraphicsView mouse move event not work to me

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 4.7k 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.
  • M Offline
    M Offline
    M_Hu
    wrote on last edited by
    #1

    I have a qgraphicsview to plot signal. I would zoom specific area with mouse clicking and rectangle drawing. So I need mouse pressed position and dragged position. For this I do such this:

    in header file:

    	class QtGuiApplication : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	QtGuiApplication(QWidget *parent = Q_NULLPTR);
    
    protected:
    	void mouseMoveEvent(QMouseEvent* event);
    	void mousePressEvent(QMouseEvent* event);
    	bool eventFilter(QObject *obj, QEvent *ev);
    
    private:
    	QPoint Zoom_point1_;
    	QPoint Zoom_point2_;
    	QGraphicsScene* scene = new QGraphicsScene();
    
    };
    

    in source file:

    	QtGuiApplication::QtGuiApplication(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	ui.graphicsView->installEventFilter(this);
    	ui.graphicsView->setMouseTracking(true);
    }
    
    bool QtGuiApplication::eventFilter(QObject * obj, QEvent * ev)
    {
    	if (obj == ui.graphicsView)
    		if (ev->type() == QEvent::MouseMove)
    		{
    			QMouseEvent *mEvent = (QMouseEvent*)ev;
    			Zoom_point2_ = mEvent->pos();
    		}
    	return false;
    }
    void QtGuiApplication::mouseMoveEvent(QMouseEvent * ev)
    {
    	Zoom_point2_ = ev->globalPos();
    	//do some thing …
    }
    
    void QtGuiApplication::mousePressEvent(QMouseEvent * ev)
    {
    	Zoom_point1_ = ev->globalPos();	
    }
    

    When I press and move mouse in graphicsview, I can recognize the clicked position but mouseMoveEvent(QMouseEvent * ev) never be called! and also obj == ui.graphicsView statement in eventFilter never be occurred.What's wrong with me? How can I fix it?

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

      Hi,

      Why are you re-implementing QMainWindow mouse related events as well as filter QGraphicsView ?

      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
      • M Offline
        M Offline
        M_Hu
        wrote on last edited by
        #3

        I want to recognize mouse event on graph area. so I think the filter must be installed on the qgraphview object. Is it wrong?

        1 Reply Last reply
        0
        • AndeolA Offline
          AndeolA Offline
          Andeol
          wrote on last edited by
          #4

          Hi,

          I think you have your event filter installation backward:

          ui.graphicsView->installEventFilter(this);
          

          Should be:

          this->installEventFilter(ui->graphicsView);
          

          So here, you are saying your mainWindow should spy on the event destined to your ui.graphicsView, while it's actually the opposite that you want.

          Developer for R++ : https://rplusplus.com/

          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