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



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



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



  • 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 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?



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.