How to add a graphicsrectitem to a graphicslayout?
-
Can't find much information on how to achieve that, so any hint is appreciated. :D
My goal is to have all items in a layout with adjustable margins between the items. The graphicsitems themself should still be scalable in height if the layout is horizontal and scalable in width if the layout is vertical.// Erstellen Sie ein QGraphicsWidget QGraphicsWidget *widget = new QGraphicsWidget(); widget->setFlag(QGraphicsItem::ItemIsMovable); QGraphicsWidget *item1 = new QGraphicsWidget; item1->setAutoFillBackground(true); QGraphicsWidget *item2 = new QGraphicsWidget; item2->setAutoFillBackground(true); // Erstellen Sie ein Layout QGraphicsRectItem *RectItem = new QGraphicsRectItem(); QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(); layout->addItem(item1); layout->addItem(RectItem); layout->addItem(item2); layout->setSpacing(50); layout->setOrientation(Qt::Vertical); // Setzen Sie das Layout für das Widget widget->setLayout(layout); // Fügen Sie das Widget zur Szene hinzu addItem(widget);
-
Read what's written here
QGraphicsLayout
is forQGraphicsWidgets
, notQGraphicsItems
. -
@StudentScripter
As @Pl45m4 has written. Which means I don't understand how you got the following through the compiler:QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(); QGraphicsRectItem *RectItem = new QGraphicsRectItem(); layout->addItem(RectItem);
? If this generated a compiler error you would surely have said so, no?
-
This example shows how a use case could look like
But as you can see, the custom items inherit from
QGraphicsLayoutItem
andQGraphicsItem
.
So to properly use aQGraphicsItem
in a layout, you have to inheritQGraphicsLayoutItem
or useQGraphicsWidget
directly... however I don't know how it fits in your existing logic/structure. It might break something... using layouts and "moving free / free resizable by user" is kinda counterproductive.