Please nominate your Qt Champions for 2021!

QGraphicsRectItem Mouse press event Problem!!!

  • i have a sub class of QgraphicsRectitem name "CLASS 1" , and re implement the mousepress event on it;

    i also have a sub class of qgraphichscene name "CLASS 2",and re implement the Mouse press event for it to ,
    and in the CLASS 2 i have some object of CLASS 1, (some rectitem in the Scene)
    Now, the problem is when i click on the Rect in the scene !!! the Function of CLASS 2 , scene, is calling!!!
    but, i want to for example move or ... the RECT!!!!1
    what can i DO ?????
    so thank you!!

  • it will be better if u post your code ...

  • this is my code:
    @class Scene : public QGraphicsScene{
    Scene(QGraphicsScene *parent = 0)
    R=new Rect ;
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    qDebug()<<"Scene Clicked!";

    class Rect :public QObject, public QGraphicsRectItem
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    qDebug()<<"Rect Clicked";


    And when i clicked on the RECT item , print "Scene clicked" !!!!

  • The default mousePressEvent() handler for QGraphicsScene dispatches to items in the scene. Since you have reimplemented the default, without calling super, no dispatch is done.

    Event dispatching for widgets is different from event dispatching for the QGraphicsView framework: events first go to the view, then the scene, then items; seemingly the reverse of widgets. But thats the default, and hidden, until you start reimplementing.

  • ok, but i want that !!!
    i want to draw shapes on the scene , and i want to Drag and move the shapes that i was drawn on the scene , How can I do that ?????? how can i receive event separately ?

  • In your scene handler, call super. That dispatches and invokes the handler for the item. When that handler returns, the scene handler continues executing.

    Drag is already supported by the "framework": You don't need to handle mousePressEvent to get drag and drop events. You need to interpret drag and drop events to mean to the user: move items (or create item, whatever you want it to mean.)

  • "call super" how can do that ? what's it's code? ,
    i can't find any thing in the DOC.

    Do you mean , when i want to drag an object, in the Scene handlers , Invoke the item's handlers manually?
    and when i suppose to draw on the scene , don't invoke that.

    yes? Do i get True?

  • Calling super is calling the super function. For example:

    @void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    // Calling super

      qDebug()<<"Scene Clicked!";


  • "Drag" doesn't necessarily mean move. Think abstractly: a Drag is a user moving the graphics pointer (mouse) with a button down. Your program can interpret that to mean "move" or "draw."

    When the user presses a mouse button and begins to move the mouse, it generates a dragBeginEvent (or something like that.) If it begins in a scene item, the framework dispatches it to the item's handler (through the default implementation of the scene's handler.) Otherwise, the event is probably ignored by the scene's handler (I'm not sure about this.)

    So if you want the user to be able to draw from anywhere, even from inside an item, you might need to write code in both item and scene handlers (reimplemented.)

Log in to reply