Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsItem And QGraphicsScene



  • Hello Friends And Qt Experts

    I am facing the problem B/W QGraphicsItem And QGraphicsScene

    follow my code
    .h File

    class Custome_Scene : public QGraphicsScene
    {
        Q_OBJECT
    public:
        Custome_Scene(QObject* parent = 0);
        void mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
        void mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
        void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
    }
    

    .cpp File

    Custome_Scene::Custome_Scene(QObject *parent)
    {
    
    }
    void Custome_Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
        qDebug() << mouseEvent->scenePos();
    }
    void Custome_Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    
    }
    void Custome_Scene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    
    }
    

    As you see my code
    when i try to get my current position of my mouse from QGraphicsScene
    it will work fine

    but when i Add Any QGraphicsItem in my Custome_Scene that time QGraphicsItem can be Movable or Selectable

    follow my code for Item Add

    Custome_Scene *customescene = new Custome_Scene ();
    QGraphicsRectItem *Item_Rect = new QGraphicsRectItem();
    Item_Rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    Item_Rect->setRect(10,10,100,100);
    Item_Rect->setZValue(1);
    Item_Rect->setVisible(true);
    customescene->addItem(Item_Rect);
    


  • @Ketan__Patel__0011
    I have tried to understand what you are saying/your problem is but I do not. In principle your code looks fine. So what is the question, can you rephrase it?



  • @JonB
    Thanks You For Reply

    As You see my code
    i was add RectangleItem in my Custome_Scene

    @Ketan__Patel__0011 said in QGraphicsItem And QGraphicsScene:

    Item_Rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);

    Accoranding to this my Item_Rect should be movable or selectable in my Custome_Scene but i can handle it.

    i can't select or move over the my Custome_Scene.


  • Lifetime Qt Champion

    @Ketan__Patel__0011 said in QGraphicsItem And QGraphicsScene:

    mousePressEvent

    Basic c++ - if you override a function and also want the functionality of the base class you should also call the base class implementation of this function.


Log in to reply