QGraphicsItem layouts



  • Unfortunately I can't see any documented information about this.
    I need a layout class like QLayout but not for widgets in widgets but for QGraphicsItem inside QGraphicsItems. I have:

    class AnotherGraphicsItem : public QGraphicsItem {
    public:
        QRectF boundingRect();
    
        void paint(QPainter *painter, ...) {
            painter->drawRect(0, 0, 30, 30);
            // ...
        }
    };
    
    class GraphicsItem : public QGraphicsItem {
    public:
        QRectF boundingRect();
    
        void paint(...) {
            for (size_t i = 0; i < 4; ++i) {
                AnotherGraphicsItem *aItem = new AnotherGraphicsItem();
                aItem->setPos(i * 20, 0);
                aItem->setParent(this);
            }
        }
    };
    
    class GraphicsView : public QGraphicsView {
    public:
        GraphicsView(){ setScene(new QGraphicsScene(this)); populate(); }
        void populate() {
            QGraphicsItem *item = new GraphicsItem();
            scene()->addItem(item);
        }
    };
    

    Briefly, my GraphicsView objects have a scene which has QGraphicsItem objects inside another QGraphicsItem objects

    QGraphicsView -> QGraphicsScene -> QGraphicsItem (GraphicsItem) -> 4x QGraphicsItem (AnotherGraphicsItem)

    The problem is that I need AnotherGraphicsItem to be aligned somehow in the outer GraphicsItem object. I've though to use QGraphicsLayout but it seems it needs to be used inside QGraphicsWidget which I am not gonna use or it works only in QGraphicsView (it has setLayout() method).

    Some good examples are also welcome.



  • You can use QGraphicsLayout classes. These classes require some information about the items they lay out, which is in QGraphicsLayoutItem.
    The simple option is to inherit from QGraphicsWidget (which inherits from QGraphicsLayoutItem), set size policies, fixed/preferred sizes, etc.
    If you don't like to do that for some reason (I don't see any, frankly), you can inherit from QGraphicsLayoutItem directly.



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