Reputation: 2772
I am trying to do this in both qlistview and qlistwiget adding is ok but removing, I can't figure out even after reading the docs. If someone could please shed some light on how to do this, I will greatly appreciate it. Here's my code below:
#!/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_())
Upvotes: 1
Views: 5614
Reputation: 2772
For the Qlistwidget, iterate through the Qlistwidget. If the item is selected in the Qlistwidget remove it via Qlistwidget.takeItem() method
def removeItems(self):
for item in self.lview.selectedItems():
self.lview.takeItem(self.lview.row(item))
For the Qlistview, iterate through qlistview rows, define the item by referencing it to a row in the qlistview via QStandardItemModel.item(row). If item is checked, remove row via QStandardItemModel.removeRow(row). The Method is recursive in order to remove multiple checked items.
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()
Upvotes: 1