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

Setting boundingRect() for a custom QGraphicsObject drawn using mouse



  • I want to draw a custom rectangle when dragged using a mouse events. Since i need additional features like resizing the rectangle, contextMenuEvents() and other stuff, i subclassed QGraphicsObject and overriden pure virtual functions. Moreover, i have a subclassed QGraphicsScene which handles mouse events.

    What do i want:

    To be able to draw custom rectangle when mouse events are triggered on QGraphicsScene. Like left mouse button->drag.
    Drawing it simply using QGraphicsRectItem works fine.

    Problem:

    How to set boundingRect() of my custom rectangle and paint() or update the rectangle as the mouse moves on the QGraphicsScene?

    Currently, i have an empty subclassed RectItem. Which looks like this:

    RectItem::RectItem(QObject* parent) :
        QGraphicsObject()
    {
    
    }
    
    QRectF RectItem::boundingRect() const
    {
          // how to set bounding rect when rectangle is drawn dynamically using mouse?
    }
    
    void RectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
         // how to update paint() and show a rectangle to the user?
    }
    

    How to implement both boundingRect() and paint()?


Log in to reply