2
votes

both events work properly on bare mainWindow but when I press inside the graphicsView ,placed inside the mainWindow, only mousePressEvent is responding. enter image description here

could anybody clarify this issue?

UPD: Here is the code

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    pix = new QPixmap("/Users/mac/Pictures/wallpaper/Rocks.jpg");
    scene->addPixmap(*pix);
}

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

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    sel_reg_beg_x = e->x();
    sel_reg_beg_y = e->y();
    qDebug() << "inside press";
}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    qDebug() << "inside move";
    sel_reg_end_x = e->x();
    sel_reg_end_y = e->y();
    this->update();

}
1
yes it's enabled in the UIIslam Salah
Try setMouseTracking(true).hank

1 Answers

0
votes

You have two options here:

  1. Derive your own graphics view from the QGraphicsView and implement the mouse move event handler there.

  2. Create the event filter and install it into your QGraphicsView's viewport (ui->graphicsView->viewport()->installEventFilter(...)). See the QObject::eventFilter() documentation.

And of course, you have to enable mouse tracking also for the QGraphicsView's viewport:

ui->graphicsView->viewport()->setMouseTracking(true);