如何从QListWidget中删除QListWidget项

时间:2014-03-20 23:25:50

标签: python qlistwidget

ListWidget中有5个项目。按下按钮打印出当前项目及其索引。似乎应该是从ListWidget中删除/删除项目的足够信息。

问题:如何从ListWidget中删除当前选定的列表项?

from PyQt4 import QtGui, QtCore
import sys, os

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidgetA = QtGui.QListWidget()
        for i in range(5):
            QtGui.QListWidgetItem( 'Item '+str(i), self.listWidgetA )

        self.listWidgetA.itemClicked.connect(self.item_clicked)

        myBoxLayout.addWidget(self.listWidgetA)

        myButton=QtGui.QPushButton("Delete")
        myBoxLayout.addWidget(myButton)
        myButton.clicked.connect(self.deleteItem) 

    def item_clicked(self, arg):
        print '\n\t selectedItem',  arg
        print '\n\t indexFromItem',  self.listWidgetA.indexFromItem(arg)

    def deleteItem(self):
        print '\n\t\t currentItem:', self.listWidgetA.currentItem()
        print '\n\t\t currentIndex:', self.listWidgetA.currentIndex()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

以下是显示删除列表项的两种方法的工作代码。谢谢大家!

from PyQt4 import QtGui, QtCore
import sys, os

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidgetA = QtGui.QListWidget()
        for i in range(5):
            QtGui.QListWidgetItem( 'Item '+str(i), self.listWidgetA )

        self.listWidgetA.itemClicked.connect(self.item_clicked)

        myBoxLayout.addWidget(self.listWidgetA)

        myButtonA=QtGui.QPushButton("Delete Item Using Approach A")
        myBoxLayout.addWidget(myButtonA)
        myButtonA.clicked.connect(self.deleteItemA) 

        myButtonB=QtGui.QPushButton("Delete Item Using Approach B")
        myBoxLayout.addWidget(myButtonB)
        myButtonB.clicked.connect(self.deleteItemB) 



    def item_clicked(self, arg):
        print '\n\t selectedItem',  arg
        print '\n\t indexFromItem',  self.listWidgetA.indexFromItem(arg)


    def deleteItemA(self):
        item = self.listWidgetA.takeItem(self.listWidgetA.currentRow())
        item = None


    def deleteItemB(self):
        model = self.listWidgetA.model()
        for selectedItem in self.listWidgetA.selectedItems():
            qIndex = self.listWidgetA.indexFromItem(selectedItem)
            print 'removing : %s' %model.data(qIndex).toString()
            model.removeRow(qIndex.row())


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())