Important: Please read the Qt Code of Conduct -

QGraphicsLayout & QGraphicsObject

  • Hi,

    I'm trying to construct a QGraphicsObject that has behavior from QGraphicsLayout (or actually QGraphicsGridLayout), such that items can be added to it. These child items are custom QGraphicsLayoutItem (& QGraphicItem). However i think i'm getting a bit confused with this approach as i'm not sure what will paint the child objects added via add item.

    I constructed a simplified example bellow:

    class Box : public QGraphicsObject, public QGraphicsGridLayout
            QRectF boundingRect() const;
            void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    class Section : public QGraphicsLayoutItem, public QGraphicsItem
            void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
           virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
            void setGeometry(const QRectF &geom);
            QRectF boundingRect() const;

    Effectively I want a custom QGraphicsObject that has a grid layout. Can someone help me out with how I could achieve this or even if i'm going about this all wrong.


  • Lifetime Qt Champion


    What you want to achieve is not really clear. Do you want to obtain with your classes the same thing as a Widget with a QGridLayout and other widgets in that layout ?