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] QGraphicsScene item moving problems
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QGraphicsScene item moving problems

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 6.4k 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.
  • X Offline
    X Offline
    xcoder
    wrote on last edited by
    #1

    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
    @

    Only a biker knows why a dog sticks his head out of a car window.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xcoder
        wrote on last edited by
        #3

        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);
        

        }
        @

        Only a biker knows why a dog sticks his head out of a car window.

        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