将自定义QWidget添加到QListWidget

时间:2018-02-08 04:03:14

标签: python pyside qlistwidget

如何将自定义qwidget添加到QListWidget以获取列表小部件功能,如选择,滚动,改进内存管理等。

我的目标是创建一个类似于许多人在Photoshop或Affinity Photo等程序中看到的图层管理器​​。我最终将扩展我的LayerWidget以获得更多控件,而不仅仅是一个简单的Label和Checkbox。

此处的前一个问题(https://stackoverflow.com/a/30802784/3156300)用户提到使用QListWidget和QListWidgetItem来创建我正在尝试做的事情。然而,在进一步研究之后,我找不到如何获取我的自定义小部件并将其附加到listWidget,就像他提到的那样。

enter image description here

Photoshop /亲和力照片

enter image description here

import sys
from PySide import QtGui, QtCore


class LayerObject(object):

    def __init__(self, **kwargs):
        self.name = kwargs.get('name', '')
        self.enabled = kwargs.get('enabled', False)


class LayerWidget(QtGui.QWidget):

    def __init__(self, layer):
        super(LayerWidget, self).__init__()
        self.resize(400, 50)

        # controls
        self.ui_enabled = QtGui.QCheckBox()
        self.ui_layername = QtGui.QLabel()
        self.ui_items = QtGui.QComboBox()
        self.ui_items.addItems(['Color','Saturation','Blend'])

        main_layout = QtGui.QHBoxLayout()
        main_layout.addWidget(self.ui_layername)
        main_layout.addWidget(self.ui_items)
        main_layout.addStretch()
        main_layout.addWidget(self.ui_enabled)
        self.setLayout(main_layout)

        # construct
        self._layer = None
        self.layer = layer


    # properties
    @property
    def layer(self):
        return self._layer

    @layer.setter
    def layer(self, value):
        self._layer== value
        self.ui_layername.setText(value.name)
        self.ui_enabled.setChecked(value.enabled)


class LayerManager(QtGui.QWidget):
    def __init__(self):
        super(LayerManager, self).__init__()
        self.resize(400, 300)

        # controls
        self.ui_scroll = QtGui.QWidget()
        self.ui_scroll_layout = QtGui.QVBoxLayout()
        self.ui_scroll.setLayout(self.ui_scroll_layout)

        self.ui_scroll_area = QtGui.QScrollArea()
        self.ui_scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.ui_scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.ui_scroll_area.setWidgetResizable(True)
        self.ui_scroll_area.setWidget(self.ui_scroll)

        main_layout = QtGui.QHBoxLayout()
        main_layout.addWidget(self.ui_scroll_area)
        self.setLayout(main_layout)


        self.add_layers()

    def add_layers(self):
        layers = [
            LayerObject(name='Layer001', enabled=False),
            LayerObject(name='Layer002', enabled=False),
            LayerObject(name='Layer003', enabled=True),
            LayerObject(name='Layer004', enabled=False),
            LayerObject(name='Layer005', enabled=True),
            LayerObject(name='Layer006', enabled=False),
            LayerObject(name='Layer007', enabled=False),
            LayerObject(name='Layer008', enabled=True),
            LayerObject(name='Layer009', enabled=False),
            LayerObject(name='Layer010', enabled=True)
        ]

        for x in layers:
            widget = LayerWidget(layer=x)
            self.ui_scroll_layout.addWidget(widget)


def main():
    app = QtGui.QApplication(sys.argv)
    ex = LayerManager()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

添加小部件必须使用方法setItemWidget(),对于此方法,您必须将方法传递给QListWidgetItem,为此您可以通过insertItem()方法添加方法。观察到的一个问题是项目的大小与小部件的大小不对应,为此您必须使用sizeHint()

class LayerManager(QtGui.QWidget):
    def __init__(self):
        super(LayerManager, self).__init__()
        self.resize(400, 300)
        self.list_widget = QtGui.QListWidget()
        main_layout = QtGui.QHBoxLayout(self)
        main_layout.addWidget(self.list_widget)
        self.add_layers()

    def add_layers(self):
        layers = [
            LayerObject(name='Layer001', enabled=False),
            LayerObject(name='Layer002', enabled=False),
            LayerObject(name='Layer003', enabled=True),
            LayerObject(name='Layer004', enabled=False),
            LayerObject(name='Layer005', enabled=True),
            LayerObject(name='Layer006', enabled=False),
            LayerObject(name='Layer007', enabled=False),
            LayerObject(name='Layer008', enabled=True),
            LayerObject(name='Layer009', enabled=False),
            LayerObject(name='Layer010', enabled=True)
        ]

        for x in layers:
            widget = LayerWidget(layer=x)
            item =  QtGui.QListWidgetItem()
            self.list_widget.insertItem(self.list_widget.count(), item)
            self.list_widget.setItemWidget(item, widget)
            item.setSizeHint(widget.sizeHint())

enter image description here