Important: Please read the Qt Code of Conduct -

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 |
            self.title = TitleLabel()  # TitleLabel(QGraphicsLayoutItem, QGraphicsItem)
            self.contents_widget = QGraphicsWidget(self)
            self.contents_layout = QGraphicsLinearLayout(Qt.Vertical)
            #   TITLE
            #   BODY
            self.body_layout = QGraphicsLinearLayout(Qt.Horizontal)
            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.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.body_layout.setAlignment(self.outputs_layout, Qt.AlignRight | Qt.AlignVCenter)

    Thanks for answers!

  • Banned

    This message was delayed for 600 seconds due to Troll issues on this forum

    If you could supply a MRE (Minimal Reproducible Example) that can just be copied/pasted and ran that would facilitate answer this question much more easily. Also are you using a UI auto-generated file or have you designed this using straight Python-Qt

  • I've found a solution I guess. It's weird though.
    I had to add


    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.

  • Banned

    @Niagarer perhaps there is something else going on other than what you found and your solution is simply a band-aid for another issue but since you have not supplied an MRE it is totally impossible to see if you are doing something wrong within your code and thus it is impossible to lend you any assistance. I tried turning your snippet into a MUC (Minimal Usable Code) example but it fails because of missing elements that are required that were not included. There might be a better answer out there if you can supply an MRE otherwise happy coding

Log in to reply