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. QGraphicsScene + Mousevents general question
Forum Updated to NodeBB v4.3 + New Features

QGraphicsScene + Mousevents general question

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 315 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • ademmlerA ademmler

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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) ?

      ademmlerA 1 Reply Last reply
      0
      • JonBJ JonB

        @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) ?

        ademmlerA Offline
        ademmlerA Offline
        ademmler
        wrote on last edited by
        #3

        @JonB thx for response:

        What I try to do is to react on an "right mouse click" within a QGraphicsViewScene.
        For that Ihm looking fr the simplest solution ... I have some minimal example - but how can I upload here?

        Regards Alex

        1 Reply Last reply
        0
        • ademmlerA Offline
          ademmlerA Offline
          ademmler
          wrote on last edited by
          #4
          #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;
          }
          
          JonBJ 1 Reply Last reply
          0
          • ademmlerA Offline
            ademmlerA Offline
            ademmler
            wrote on last edited by
            #5
            #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
            
            1 Reply Last reply
            0
            • ademmlerA Offline
              ademmlerA Offline
              ademmler
              wrote on last edited by
              #6
              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
              
              1 Reply Last reply
              0
              • ademmlerA ademmler
                #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;
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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 your QGraphicsScene *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.

                ademmlerA 1 Reply Last reply
                0
                • JonBJ JonB

                  @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 your QGraphicsScene *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.

                  ademmlerA Offline
                  ademmlerA Offline
                  ademmler
                  wrote on last edited by
                  #8

                  @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.

                  JonBJ 1 Reply Last reply
                  0
                  • ademmlerA ademmler

                    @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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @ademmler
                    In Qt I think you will find the convention that methods with event in the name tend to be protected 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?

                    ademmlerA 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @ademmler
                      In Qt I think you will find the convention that methods with event in the name tend to be protected 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?

                      ademmlerA Offline
                      ademmlerA Offline
                      ademmler
                      wrote on last edited by
                      #10

                      @JonB said in QGraphicsScene + Mousevents general question:

                      eventFilter

                      I will soon and let you know - thank you.

                      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