如何在qlistview中正确添加和删除项目?

时间:2017-04-28 09:37:46

标签: python pyqt

我正在尝试在qlistview和qlistwiget中执行此操作添加即可,但删除,即使在阅读文档后我也想不通。如果有人能说明如何做到这一点,我将非常感激。这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui


class Try(QtGui.QDialog):
    def __init__(self):
        super(Try, self).__init__()
        self.setWindowTitle("Trying stuff")
        self.list_view = QtGui.QListView(self)
        self.lview = QtGui.QListWidget(self)
        self.EditText = QtGui.QLineEdit()
        self.EditText.setPlaceholderText("add stuff")
        self.EditText.setMaximumWidth(200)
        self.setWindowFlags(QtCore.Qt.Window)
        self.model = QtGui.QStandardItemModel(self.list_view)
        self.resize(600, 500)
        self.font = QtGui.QFont()
        self.font.setBold(True)
        self.font2 = QtGui.QFont()
        self.font2.setBold(False)
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.red)
        self.label = QtGui.QLabel()
        self.buttonRemove = QtGui.QPushButton()
        self.buttonAdd = QtGui.QPushButton()
        self.buttonCommit = QtGui.QPushButton()
        self.buttonRemove.setText("Remove from list")
        self.buttonRemove.clicked.connect(self.removeItems)
        self.buttonAdd.setText("Add to list")
        self.buttonAdd.clicked.connect(self.addItems)
        self.buttonCommit.setText("print all item in list")

        horizontalLayout = QtGui.QHBoxLayout()
        horizontalLayout.addWidget(self.EditText)
        horizontalLayout.addWidget(self.buttonAdd)

        horizontalLayout.setAlignment(QtCore.Qt.AlignRight)
        horizontalLayout2 = QtGui.QHBoxLayout()
        horizontalLayout2.addWidget(self.buttonRemove)
        horizontalLayout2.addWidget(self.buttonCommit)
        horizontalLayout2.setAlignment(QtCore.Qt.AlignRight)

        verticalLayout = QtGui.QVBoxLayout(self)
        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.list_view)
        verticalLayout.addWidget(self.lview)
        verticalLayout.addWidget(self.label)
        verticalLayout.addLayout(horizontalLayout2)


    def addItems(self):
        x = self.EditText.text()
        self.lview.addItem(x)
        self.lview.setAutoScroll(True)
        item = QtGui.QStandardItem(x)
        item.setCheckable(True)
        item.setCheckState(QtCore.Qt.Unchecked)
        self.model.appendRow(item)
        self.list_view.setModel(self.model)
        self.EditText.clear()

    def removeItems(self):
        x = self.EditText.text()
        item = QtGui.QStandardItem(x)
        todelete = self.list_view.selectionModel().currentIndex().data().toString()
        print todelete
        self.model.removeRow(todelete)
        self.lview.takeItem(item.row())
        #self.model.removeRow(item.row())

        self.list_view.setModel(self.model)
        self.EditText.clear()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    t = Try()
    t.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

对于Qlistwidget,      迭代Qlistwidget。如果选择了该项目      Qlistwidget通过Qlistwidget.takeItem()方法

删除它
def removeItems(self):
    for item in self.lview.selectedItems():
        self.lview.takeItem(self.lview.row(item))

对于Qlistview,遍历qlistview行,通过QStandardItemModel.item(行)将项引用到qlistview中的行来定义项。 如果选中了item,则通过QStandardItemModel.removeRow(row)删除行。该 方法是递归的,以便删除多个已检查的项目。

for row in xrange(self.model.rowCount()):
        item = self.model.item(row)  
        if item and item.checkState() == QtCore.Qt.Checked:
            self.model.removeRow(row)
            self.removeItems()