QGraphicsScene + Mousevents general question
-
Hi Pros,
do I always need to override Mouse events and build an Custom::Scene class if I want to get those on an QtGraphicsscene? I got this impression while googling for advice and trial code.
or ist there a simple way/slot to access an right mouse click on an QGraphicsScene?
Regards Alex
-
Hi Pros,
do I always need to override Mouse events and build an Custom::Scene class if I want to get those on an QtGraphicsscene? I got this impression while googling for advice and trial code.
or ist there a simple way/slot to access an right mouse click on an QGraphicsScene?
Regards Alex
@ademmler said in QGraphicsScene + Mousevents general question:
or ist there a simple way/slot to access an right mouse click on an QGraphicsScene?
Do you mean by overriding QGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) ?
-
@ademmler said in QGraphicsScene + Mousevents general question:
or ist there a simple way/slot to access an right mouse click on an QGraphicsScene?
Do you mean by overriding QGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) ?
-
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->graphicsView->setEnabled(true); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_openButton_clicked() { ui->statusbar->showMessage("Opening file ...", 0); QString imagePath = QFileDialog::getOpenFileName( this, tr("Open File"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("TIFF (*.tif *tiff);;JPEG (*.jpg *.jpeg);;PNG (*.png)" ) ); showImage(imagePath); } void MainWindow::showImage(QString imagePath) { imageObject = new QImage(); imageObject->load(imagePath); image = QPixmap::fromImage(*imageObject); scene = new QGraphicsScene(this); pixmap = scene->addPixmap(image); scene->setSceneRect(image.rect()); ui->graphicsView->setScene(scene); pixmap->setFlag(QGraphicsItem::ItemIsMovable); ui->statusbar->showMessage("Idle.", 0); } void MainWindow::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) { qDebug() << "QGS Event: " << contextMenuEvent << Qt::endl; }
-
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsScene> #include <QGraphicsPixmapItem> #include <QFileDialog> #include <QStandardPaths> #include <QDebug> #include <QGraphicsSceneMouseEvent> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); protected: void contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent); private slots: void on_openButton_clicked(); void showImage(QString imagePath); private: Ui::MainWindow *ui; QPixmap image; QImage *imageObject; QGraphicsScene *scene; QGraphicsPixmapItem *pixmap; }; #endif // MAINWINDOW_H
-
TEMPLATE = app TARGET = QtMinimalScene QT += core gui widgets CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 INCLUDEPATH += . SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
-
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->graphicsView->setEnabled(true); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_openButton_clicked() { ui->statusbar->showMessage("Opening file ...", 0); QString imagePath = QFileDialog::getOpenFileName( this, tr("Open File"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("TIFF (*.tif *tiff);;JPEG (*.jpg *.jpeg);;PNG (*.png)" ) ); showImage(imagePath); } void MainWindow::showImage(QString imagePath) { imageObject = new QImage(); imageObject->load(imagePath); image = QPixmap::fromImage(*imageObject); scene = new QGraphicsScene(this); pixmap = scene->addPixmap(image); scene->setSceneRect(image.rect()); ui->graphicsView->setScene(scene); pixmap->setFlag(QGraphicsItem::ItemIsMovable); ui->statusbar->showMessage("Idle.", 0); } void MainWindow::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) { qDebug() << "QGS Event: " << contextMenuEvent << Qt::endl; }
@ademmler said in QGraphicsScene + Mousevents general question:
void MainWindow::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) { qDebug() << "QGS Event: " << contextMenuEvent << Qt::endl; }
What is your question? This is the context menu event for your
MainWindow
. Not for yourQGraphicsScene *scene
. If that is what you wanted.If you don't want to sub-class (is that the issue?), you may get the right click/context menu via an
eventFilter()
, I don't know. -
@ademmler said in QGraphicsScene + Mousevents general question:
void MainWindow::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) { qDebug() << "QGS Event: " << contextMenuEvent << Qt::endl; }
What is your question? This is the context menu event for your
MainWindow
. Not for yourQGraphicsScene *scene
. If that is what you wanted.If you don't want to sub-class (is that the issue?), you may get the right click/context menu via an
eventFilter()
, I don't know.@JonB said in QGraphicsScene + Mousevents general question:
If you don't want to sub-class (is that the issue?), you may get the right click/context menu via an eventFilter(), I don't know.
I asked in the beginning if "subclassing" is mandatory to access QGraphicsScene events ...
It seems like that this is the only way. -
@JonB said in QGraphicsScene + Mousevents general question:
If you don't want to sub-class (is that the issue?), you may get the right click/context menu via an eventFilter(), I don't know.
I asked in the beginning if "subclassing" is mandatory to access QGraphicsScene events ...
It seems like that this is the only way.@ademmler
In Qt I think you will find the convention that methods withevent
in the name tend to beprotected
members which you have to override (and therefore subclass) if you need to modify/be notified of them. That differs from the many signal cases in classes where you can just add your own slot(s) without subclassing.As I said earlier, it's usually also possible to go via an
eventFilter
to avoid sub-classing. Did you look into that as an alternative? -
@ademmler
In Qt I think you will find the convention that methods withevent
in the name tend to beprotected
members which you have to override (and therefore subclass) if you need to modify/be notified of them. That differs from the many signal cases in classes where you can just add your own slot(s) without subclassing.As I said earlier, it's usually also possible to go via an
eventFilter
to avoid sub-classing. Did you look into that as an alternative?