带有编辑器的qlistview的pyqt自定义项目委托

时间:2018-07-17 19:36:27

标签: python-2.7 pyqt5 qlistview pyside2 qstyleditemdelegate

我想通过使用Qlistwidget将应用程序重写为带有模型/视图数据的QListView,因为这样做更有意义。在我的旧应用程序中,我有一个子类的Listwidget,它使用自己的itemWidget添加项目,如下所示:

def createNewLink(self, nodename, nodeclass, nodelabel="", stateHidden=True, stateLinktype="connector"):

    # Check wether item already exists
    if nodename not in self.getAllListNodenames():
        item = QtWidgets.QListWidgetItem(self)
        item_widget = NodeLinkItemWidgetUI.Ui_Form(nodename, nodeclass, nodelabel)
        #### ...................####

        item.setSizeHint(QtCore.QSize(130, 160))
        self.addItem(item)
        self.setItemWidget(item, item_widget)

        item_widget.comboBox_type.currentIndexChanged.connect(self.itemupdate_linktype)
        item_widget.checkBox_hide.stateChanged.connect(self.itemupdate_hidden)

问题:如何创建自定义窗口小部件集合(以控制各种类型的数据)并将其作为“项目”添加到列表视图窗口小部件。我需要每个项目的图标,描述节点名称字符串的标签,控制stateLinktype变量的组合框和控制stateHidden的复选框。 那么如何通过项目委托实现项目小部件,它不仅显示相应项目的数据,还允许用户即时对其进行编辑?。我已经读过这个http://doc.qt.io/qt-5/model-view-programming.html,但是它和Google并没有使我接近于弄清楚它。这是listwidget enter image description here中正在运行的应用程序的图片,这是我现在用于模型视图应用程序的基本代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ConnectorUI.ui'
#
# Created: Sat Jul 14 14:56:27 2018
#      by: pyside2-uic  running on PySide2 5.9.0a1.dev1526649435
#
# WARNING! All changes made in this file will be lost!

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(1061, 201)
        Dialog.setWindowTitle("Connecthor")
        self.lineEdit_searchLinks = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_searchLinks.setGeometry(QtCore.QRect(420, 10, 211, 25))
        self.lineEdit_searchLinks.setPlaceholderText("search for link")
        self.lineEdit_searchLinks.setObjectName("lineEdit_searchLinks")
        self.label_filter = QtWidgets.QLabel(Dialog)
        self.label_filter.setGeometry(QtCore.QRect(10, 13, 41, 17))
        self.label_filter.setText("filter:")
        self.label_filter.setObjectName("label_filter")
        self.scrollArea = QtWidgets.QScrollArea(Dialog)
        self.scrollArea.setGeometry(QtCore.QRect(10, 40, 1041, 151))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1039, 149))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.listView_links = QtWidgets.QListView(self.scrollAreaWidgetContents)
        self.listView_links.setGeometry(QtCore.QRect(0, 0, 1040, 150))
        self.listView_links.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.listView_links.setDragEnabled(True)
        self.listView_links.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
        self.listView_links.setDefaultDropAction(QtCore.Qt.MoveAction)
        self.listView_links.setAlternatingRowColors(True)
        self.listView_links.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.listView_links.setFlow(QtWidgets.QListView.LeftToRight)
        self.listView_links.setObjectName("listView_links")
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.comboBox_filterForNodeClass = QtWidgets.QComboBox(Dialog)
        self.comboBox_filterForNodeClass.setGeometry(QtCore.QRect(55, 10, 79, 25))
        self.comboBox_filterForNodeClass.setCurrentText("all")
        self.comboBox_filterForNodeClass.setObjectName("comboBox_filterForNodeClass")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(0, "all")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(1, "read / write")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(2, "dot")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(3, "3d")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(4, "merges")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(5, "roto")
        self.comboBox_filterForNodeClass.addItem("")
        self.comboBox_filterForNodeClass.setItemText(6, "channel")
        self.toolButton = QtWidgets.QToolButton(Dialog)
        self.toolButton.setGeometry(QtCore.QRect(1025, 10, 26, 24))
        self.toolButton.setObjectName("toolButton")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        self.toolButton.setText(QtWidgets.QApplication.translate("Dialog", "...", None, -1))

    from collections import OrderedDict
import sys
import os
import json
#import nuke, os, re
from PySide2 import QtCore, QtGui, QtWidgets
from ConnectorUI import Ui_Dialog

class Connector(QtWidgets.QDialog, Ui_Dialog):
    #
    allowedNodes = ["Read", "Write", "Merge", "Keymix", "ChannelMerge", "Roto", "RotoPaint", "Copy", "Shuffle", "PostageStamp", "Camera", "Camera2", "ScanlineRender", "Connector", "ReadGeo", "ReadGeo2"]
    script_path = os.path.dirname(os.path.realpath(__file__))

    #constructor
    def __init__(self, parent=None):
        super(Connector, self).__init__(parent)
        self.setupUi(self)
        self.setFixedSize(self.size())

        linklist = []
        for i in range(1,100):
            link = Link("Camera"+str(i),"Camera")
            linklist.append(link)
        print linklist
        model = LinkListModel(linklist)
        self.listView_links.setModel(model)


        #set filter
        #self.comboBox_filter.currentIndexChanged.connect(self.listItemFilter)
        #self.btn_createlink.clicked.connect(self.createLinkFromNodegraph)


class LinkListModel(QtCore.QAbstractListModel):
    def __init__(self, items = [], parent = None):
        QtCore.QAbstractListModel.__init__(self,parent)
        self.__items = items #this sets our data

    #have to be reimplemented
    def data(self, index, role):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            value = self.__items[row]
            return value.getNodename()

    def rowCount(self,parent):
        return len(self.__items)

class Link():
    def __init__(self, nodename, nodeclass, nodelabel="", stateHidden=True, stateLinktype="dot"):
        self.__nodename = nodename
        self.__nodeclass = nodeclass
        self.__nodelabel = nodelabel
        self.__stateHidden = stateHidden
        self.__stateLinktype = stateLinktype

    def getNodename(self):
        return self.__nodename
    def getNodeclass(self):
        return  self.__nodeclass
    def getNodelabel(self):
        return  self.__nodelabel
    def getNodehideinput (self):
        return self.__stateHidden
    def getLinktype(self):
        return self.__stateLinktype


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    main = Connector()
    main.show()
    sys.exit(app.exec_())

0 个答案:

没有答案