QGraphicsLayout does not expand like QLayouts
-
Hi
I am using some QGraphicsLinearLayouts here and want them to always scale to the max, adding space between the contents (which is what a normal QLayout does). Shouldn't this be done via the size policy?In this example:
The text at the top is the title label (QGraphicsLayoutItem, QGraphicsItem). The box beneath it is the body (a QGraphicsLinearLayout holding two other linear layouts, one for inputs, one for outputs). Title label and body layout are together in a vertical linear layout. Here, the min title width is larger than the min body width, so the body layout should scale to that width, keeping its contents at the edges left and right. Same with the outputs layout of the body here. The right labels in the picture should be positioned at the top, middle, and the third at the bottom of the body layout.
I tried setting the size policy of the layouts to Maximum or Expanding, both didn't work. I am sure this is basic, but I don't get it.
This is how I create the layouts:class NodeInstance(QGraphicsItem): def __init__(self): super(NodeInstance, self).__init__() self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemSendsScenePositionChanges) self.setAcceptHoverEvents(True) self.title = TitleLabel() # TitleLabel(QGraphicsLayoutItem, QGraphicsItem) # CONTENT WIDGET AND LAYOUTS self.contents_widget = QGraphicsWidget(self) self.contents_layout = QGraphicsLinearLayout(Qt.Vertical) self.contents_layout.setSpacing(10) # TITLE self.contents_layout.addItem(self.title) # BODY self.body_layout = QGraphicsLinearLayout(Qt.Horizontal) self.body_layout.setSpacing(30) self.body_layout.setSizePolicy(QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)) # add inputs self.inputs_layout = QGraphicsLinearLayout(Qt.Vertical) for i in range(5): inp = InputPortInstance() # InputPortInstance(QGraphicsLinearLayout) - holds circle and label, see picture self.inputs_layout.addItem(inp) self.body_layout.addItem(self.inputs_layout) self.body_layout.setAlignment(self.inputs_layout, Qt.AlignLeft | Qt.AlignVCenter) # add outputs self.outputs_layout = QGraphicsLinearLayout(Qt.Vertical) for i in range(3): out = OutputPortInstance() # OutputPortInstance(QGraphicsLinearLayout) - holds circle and label, see picture self.outputs_layout.addItem(out) self.body_layout.addItem(self.outputs_layout) self.body_layout.setAlignment(self.outputs_layout, Qt.AlignRight | Qt.AlignVCenter) self.contents_layout.addItem(self.body_layout) self.contents_widget.setLayout(self.contents_layout) ...
Thanks for answers!
-
I've found a solution I guess. It's weird though.
I had to addlayout.addStretch()
between adding two layouts.
There is so much about these layouts that just does not seem to work. Removing an item: still visible and the layout doesn't adjust it's size. Deleting the item: still visible (!?). Setting a SizePolicy: no effect at all (basically counts for everything size-related). And a lot of other small things that make me wonder. I probably just don't get it.