PyQt QGraphicsLayout Stacking

时间:2017-03-25 18:18:19

标签: python pyqt pyqt4 qgraphicsscene qgraphicsitem

我正在尝试在Pyqt4中创建一个qgraphicslayout。我有一系列QGraphicsLayoutItems,但它们似乎是在布局而不是间距堆叠。在左边,这些物品都在彼此的顶部。

Example of the Stacking

由于QGraphicsLayoutItem是抽象的,我在以下类中重写它:

class AttributeGFX(QtGui.QGraphicsLayoutItem):
"""Wrapper."""

def __init__(self,
             n_x,
             n_y,
             n_scene,
             n_settings,
             n_name,
             n_type,
             n_io):
    """Init."""
    super(AttributeGFX, self).__init__()
    self.io = n_io
    self.name = n_name

    # Use same object for inputs and outputs
    self.is_input = True
    if "output" in n_io:
        self.is_input = False


    if self.is_input:
        self.disp_settings = n_settings.display_settings['INPUTS']
    else:
        self.disp_settings = n_settings.display_settings['OUTPUTS']

    if n_type not in self.disp_settings.keys():
        self.disp_settings = self.disp_settings['default']
    else:
        self.disp_settings = self.disp_settings[n_type]


    self.gitem = _AttributeGFX(n_x,
                               n_y,
                               n_scene,
                               n_settings,
                               n_name,
                               n_type,
                               n_io)
    self.setGraphicsItem(self.gitem)

def sizeHint(self, z, sizeh):
    """Get the size."""
    boundingrec = self.gitem.boundingRect()
    r = QtCore.QSizeF()
    r.setHeight(boundingrec.height())
    r.setWidth(boundingrec.width())
    return r

我认为可能是尺寸提示,所以我在QSizeF对象中尝试了大型浮动,而不是布局项目中图形项目的边界记录。

有什么想法吗?下面是我创建布局和添加项目的地方。

class _GraphicsLayout (QtGui.QGraphicsWidget):
    """Provide structure for organizing elements."""

    def __init__(self, spacing, parent):
        """Init."""
        super(_GraphicsLayout, self).__init__(parent)
        self.layout = QtGui.QGraphicsLinearLayout()
        self.layout.setSpacing(spacing)
        self.layout.setOrientation(QtCore.Qt.Vertical)
        self.layout.addStretch()
        self.setLayout(self.layout)
        self.name = self.parentItem().name
        self.items = {}
        self.spacing = spacing

    def add_item(self, item):
        """Add a new item to the layout and transform it into position."""
        '''
        trans = self.spacing * len(self.items) - 1 + item.height
        item.setParentItem(self)
        item.translate(0, trans)
        '''
        self.items[item.name] = item
        self.layout.addItem(self.items[item.name])
        self.layout.setSpacing(self.spacing)

编辑:我在QGraphicsLayoutItems上打印了parentLayoutItem,它们在布局中。

1 个答案:

答案 0 :(得分:0)

我发现你不仅需要覆盖qGraphicsLayoutItem类中的sizeHint函数,还需要覆盖setGeometry函数。我不确定是否需要覆盖updateGeometry函数,但我们会看到:ExamplesDocs

最终成绩:

class AttributeGFX(QtGui.QGraphicsLayoutItem):
    """Wrapper."""

    def __init__(self,
                 n_x,
                 n_y,
                 n_scene,
                 n_settings,
                 n_name,
                 n_type,
                 n_io):
        """Init."""
        super(AttributeGFX, self).__init__()
        self.io = n_io
        self.name = n_name

        # Use same object for inputs and outputs
        self.is_input = True
        if "output" in n_io:
            self.is_input = False


        if self.is_input:
            self.disp_settings = n_settings.display_settings['INPUTS']
        else:
            self.disp_settings = n_settings.display_settings['OUTPUTS']

        if n_type not in self.disp_settings.keys():
            self.disp_settings = self.disp_settings['default']
        else:
            self.disp_settings = self.disp_settings[n_type]


        self.gitem = _AttributeGFX(n_x,
                                   n_y,
                                   n_scene,
                                   n_settings,
                                   n_name,
                                   n_type,
                                   n_io)
        self.setGraphicsItem(self.gitem)
        self.setMinimumHeight(3)

    def sizeHint(self, z, sizeh):
        """Get the size."""
        boundingrec = self.gitem.boundingRect()
        r = QtCore.QSizeF()
        r.setHeight(boundingrec.height())
        r.setWidth(boundingrec.width())
        return r

    def setGeometry(self, x):
        """Set geo size."""
        self.gitem.setPos(x.topLeft())