PyQt QGraphicsLayout 堆叠
PyQt QGraphicsLayout Stacking
我正在尝试在 Pyqt4 中创建一个 qgraphicslayout。我有一系列 QGraphicsLayoutItems,但它们似乎在布局中堆叠而不是间距。在左侧,所有项目都在彼此之上。
由于 QGraphicsLayoutItem 是抽象的,我在下面覆盖了它 class:
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,它们在布局中。
我发现不仅需要重写qGraphicsLayoutItem中的sizeHint函数class,还需要重写setGeometry函数。我不确定是否需要重写 updateGeometry 函数,但我们会看到:Examples and Docs
决赛 Class:
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())
我正在尝试在 Pyqt4 中创建一个 qgraphicslayout。我有一系列 QGraphicsLayoutItems,但它们似乎在布局中堆叠而不是间距。在左侧,所有项目都在彼此之上。
由于 QGraphicsLayoutItem 是抽象的,我在下面覆盖了它 class:
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,它们在布局中。
我发现不仅需要重写qGraphicsLayoutItem中的sizeHint函数class,还需要重写setGeometry函数。我不确定是否需要重写 updateGeometry 函数,但我们会看到:Examples and Docs
决赛 Class:
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())