0
votes

I'm studying qt libraries for a project in my university. I'm trying to create a simple window with just a graphicsView into it, catching mouse coordinates only when the cursor is inside the graphicsView. I tried first with mouseMoveEvent function, but I wasn't able to get it working. I tried then with filter, and even with them it is not working. I am really stuck over this thing, I provide here the code. The problem is that mousePressEvent works fine only OUTSIDE the graphicsView, always printing "1. X: ... Y: ...". I also created a new class for graphicsView, as follows:

//myGraphicsView.h
class MyGraphicsView : public QWidget
{
  Q_OBJECT
public:
  explicit MyGraphicsView(QWidget *parent = 0);

protected:
  void mousePressEvent(QMouseEvent* ev);
  bool eventFilter(QObject *obj, QEvent *e);

private:
  QGraphicsView *gv;
  QVBoxLayout *layout;
};

myGraphicView.cpp is as follows:

MyGraphicsView::MyGraphicsView(QWidget *parent) :
  QWidget(parent)
{
  gv = new QGraphicsView;
  layout = new QVBoxLayout;
  layout->addWidget(gv);
  setLayout(layout);

  gv->setMouseTracking(true);
  setMouseTracking(true);
}

void MyGraphicsView::mousePressEvent(QMouseEvent* event)
{
  qDebug() << "1. X: " << event->pos().x() << "; Y: " << event->pos().y() << endl;
}

bool MyGraphicsView::eventFilter(QObject *obj, QEvent *e)
{
  if (e->type() == QEvent::MouseButtonPress)
  {
    QMouseEvent *mouseEvent = (QMouseEvent *) e;
    qDebug() << "3. X: " << mouseEvent->x() << "; Y: " << mouseEvent->y() << endl;
    return true;
  }
  return false;
}

MainWindows.h is as follows:

    namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

protected:
  void mousePressEvent(QMouseEvent* ev);

private:
  Ui::MainWindow *ui;
  MyGraphicsView *v;
};

MainWindows.cpp is as follows:

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  v = new MyGraphicsView;
  setMouseTracking(true);
  setCentralWidget(v);

  setWindowTitle("Simple mouse tracker");
  resize(300, 300);
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent* ev)
{
  qDebug() << "2. X: " << ev->pos().x() << "; Y: " << ev->pos().y() << endl;
}
1

1 Answers

0
votes

I would recommend the following way:

Create your own Class inherated from QGraphicsView

mygraphicview.h

#ifndef MYGRAPHICVIEW_H
#define MYGRAPHICVIEW_H

#include <QGraphicsView>

class MyGraphicView : public QGraphicsView
{
     public:
     MyGraphicView(QWidget *parent);

     protected:
     virtual void mouseMoveEvent(QMouseEvent *e);
};

#endif // MYGRAPHICVIEW_Hcode here

mygraphicview.cpp

#include "mygraphicview.h"
#include <QMouseEvent>
#include <QDebug>

MyGraphicView::MyGraphicView(QWidget *parent) : QGraphicsView(parent)
{
     setMouseTracking(true);
}

void MyGraphicView::mouseMoveEvent(QMouseEvent *e)
{
    qDebug() << e->pos().x() << e->pos().y();
}

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    MyGraphicView *v = new MyGraphicView(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}