PyQt QGraphicsLayout堆叠

2024-09-29 22:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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对象中使用大浮点,而不是布局项中graphicsitem的bounding rec。在

知道怎么回事吗?下面是我创建布局和添加项目的地方。在

^{pr2}$

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


Tags: 项目nameioselfinputifsettingsis
1条回答
网友
1楼 · 发布于 2024-09-29 22:29:47

我发现不仅需要重写qGraphicsLayoutItem类中的sizeHint函数,还需要重写setGeometry函数。我不确定是否需要重写updateGeometry函数,但我们将看到:Examples和{a2}

最终课程:

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())

相关问题 更多 >

    热门问题