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. [SOLVED] Detecting mouse click in scene (QGraphicsScene) help
QtWS25 Last Chance

[SOLVED] Detecting mouse click in scene (QGraphicsScene) help

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 6.6k Views
  • 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.
  • D Offline
    D Offline
    Demmy
    wrote on last edited by
    #1

    I'm attempting to get to know some QGraphicsScene options and it's going tough on me.

    What I'm trying to do: When I click on scene, I want a rectangle to be shown/hidden (toggle).

    What I've managed to do so far: If I use QMouseEvent instead of QGraphicsMouseEvent it works well, with the exception of it being toggled even if I click outside the scene. If I use QGraphicsMouseEvent it does not trigger.

    pomocnidrugi.cpp
    @#include "pomocnidrugi.h"
    #include "ui_pomocnidrugi.h"
    #include "QGraphicsItem.h"

    bool jePressed=false;
    pomocnidrugi::pomocnidrugi(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::pomocnidrugi)
    {
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    
    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackPen(Qt::black);
    blackPen.setWidth(6);
    
    ellipse = scene->addEllipse(10,10,100,100,blackPen, redBrush);
    rectangle = scene ->addRect(10,210,100,310,blackPen,blueBrush);
    rectangle->setFlag(QGraphicsItem::ItemIsMovable);
    

    }

    pomocnidrugi::~pomocnidrugi()
    {
    delete ui;
    }
    void pomocnidrugi::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    if(jePressed){rectangle->show(); jePressed=false;}
    else{rectangle->hide(); jePressed=true;}
    }
    @

    pomocnidrugi.h
    @#ifndef POMOCNIDRUGI_H
    #define POMOCNIDRUGI_H

    #include <QWidget>
    #include <QGraphicsScene>

    namespace Ui {
    class pomocnidrugi;
    }

    class pomocnidrugi : public QWidget
    {
    Q_OBJECT

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

    private:
    Ui::pomocnidrugi *ui;
    QGraphicsScene *scene;
    QGraphicsEllipseItem *ellipse;
    QGraphicsRectItem *rectangle;
    //QTimer *timer;

    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);

    };

    #endif // POMOCNIDRUGI_H@

    This is ofcourse non-working state (again, it does trigger if I use QMouseEvent instead of QGraphicsSceneMouseEvent - but not completely as intended).

    What am I supposed to do to make it work? I would appriciate the easiset solution possible, I'm still in the early phase of learning.

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      Is the goal to detect a click anywhere within the [view of the] scene, or within an individual item in the scene?

      Within the context of a scene displayed by a view, QGraphicsSceneMouseEvents are delivered to items within the scene and the scene itself. QMouseEvents are delivered to the view.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Demmy
        wrote on last edited by
        #3

        [quote author="jeremy_k" date="1402970867"]Is the goal to detect a click anywhere within the [view of the] scene, or within an individual item in the scene?

        Within the context of a scene displayed by a view, QGraphicsSceneMouseEvents are delivered to items within the scene and the scene itself. QMouseEvents are delivered to the view.[/quote]

        The goal is anywhere within a scene (or graphicsView box). What do you propose I do? QMouseEvent delivered but it triggered even outside the scene/graphicsView (as long as it's in that window). QGraphicsSceneMouseEvent does not trigger at all, inside or outside the scene.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JvdGlind
          wrote on last edited by
          #4

          I am not entirely sure, but you are implementing mousePressEvent(QGraphicsSceneMouseEvent *event); in QWidget, not in QGraphicsScene. QWidget does not have a virtual function for QGraphicsSceneMouseEvent, but does have one for QMouseEvent, so my guess is that is why it is called only for the latter.

          Jeffrey VAN DE GLIND
          Principle Consultant @ Nalys
          www.nalys-group.com

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Demmy
            wrote on last edited by
            #5

            [quote author="JvdGlind" date="1403015680"]I am not entirely sure, but you are implementing mousePressEvent(QGraphicsSceneMouseEvent *event); in QWidget, not in QGraphicsScene. QWidget does not have a virtual function for QGraphicsSceneMouseEvent, but does have one for QMouseEvent, so my guess is that is why it is called only for the latter.[/quote]

            Thanks, I thought it would be because of that. However, how should I pull it out at this point? I tried another example with creating a separate class and header for a scene the way you desribed, which worked with QGraphicsSceneMouseEvent. Is there any workaround in this case?

            1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              Another option is to install an event filter on the view. See "QObject::installEventFilter":http://qt-project.org/doc/qt-5/qobject.html#installEventFilterfor details.

              There's also QGraphicsItem::installSceneEventFilter(), although it sounds like handling at the view level is more appropriate.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jerobarraco
                wrote on last edited by
                #7

                thanks for the answer jeremy. thats cleaner than implementing a subclass of graphicsview just to trap an event

                ~namida de ashita ga mienai~

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jerobarraco
                  wrote on last edited by
                  #8

                  thanks for the answer jeremy. thats cleaner than implementing a subclass of graphicsview just to trap an event

                  ~namida de ashita ga mienai~

                  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