[SOLVED] QGraphicsScene item moving problems



  • Hello Everyone,

    Could someone please explain me how to properly override the mouse events on custom QGraphicScene?
    The thing is more or less everything is working for me, I can detect where I clicked and so on, but when I try to move the objects what I have on QGraphicsScene, I have to double click them to select them, and then can I only move them. As soon as I get rid of the mouse event functions, I can normally select and move my objects on the scene.

    Anyway here is the code:

    diagramscene.cpp
    @#include "diagramscene.h"

    DiagramScene::DiagramScene(QObject *parent)
    : QGraphicsScene(parent)
    {
    myMode = MoveItem;
    }

    void DiagramScene::setMode(Mode newMode)
    {
    myMode = newMode;
    }

    void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
    }

    void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    qDebug() << mouseEvent->scenePos() << " " << myMode << " => " << MoveItem ;
    if (mouseEvent->button() != Qt::LeftButton)
    return;

    blockItem *item;
    switch(myMode)
    {
        case InsertItem:
             item = new blockItem(QRectF(mouseEvent->scenePos().x()-25,mouseEvent->scenePos().y()-25,50,50));
             addItem(item);
             item->setFlag(QGraphicsItem::ItemIsMovable, true);
             emit itemInserted();
             break;
        default: ;
    }
    

    }
    @

    diagramscene.h
    @#ifndef DIAGRAMSCENE_H
    #define DIAGRAMSCENE_H

    #include <QGraphicsScene>
    #include <QGraphicsSceneMouseEvent>
    #include <QDebug>

    #include "blockitem.h"

    class DiagramScene : public QGraphicsScene
    {
    Q_OBJECT

    public:
    enum Mode { InsertItem, InsertLine, InsertText, MoveItem };
    DiagramScene(QObject *parent = 0);

    public slots:
    void setMode(Mode newMode);

    signals:
    void itemInserted();

    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);

    private:
    Mode myMode;
    bool mouseButtonDown;
    QPointF startPoint;
    };

    #endif // DIAGRAMSCENE_H
    @



  • You're not calling the base class implementation of mousePressEvent. I guess from your code that you'll want to call it in all cases except when inserting a new item.



  • Thank you very much. Makes sense :)

    @void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    //my scene stuff here

    //Call the base class implementation of mouse press event
    GraphicsScene::mousePressEvent(mouseEvent);
    

    }
    @


Log in to reply
 

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