There are two types of rows one is Pizza and the other one is Client.
def addClient(self):
self.listWidget.clear()
self.listWidget.addItem("client")
When new row 'Client' added i can clear all other rows but when i add another Client my first client row is getting deleted too i want the first client to not be affected by clear function.Example of rows;
-Client(first)
-Pizza
-Pizza
When i add new Client;
-Client(first)
-Client(second)
When i add couple of pizza rows;
-Client(first)
-Client(second)
-Pizza
-Pizza
Finally when i add another Client;
-Client(first)
-Client(second)
-Client(third)
As i CLEARLY show it i want to keep every Client rows when i use clear function and clear any other rows.
You do not have to use clear()
, what you have to do is filter the items that have "Pizza" as text and delete them one by one from the QListWidget:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.list_widget = QtWidgets.QListWidget()
client_btn = QtWidgets.QPushButton("add client")
client_btn.clicked.connect(self.add_client)
pizza_btn = QtWidgets.QPushButton("add pizza")
pizza_btn.clicked.connect(self.add_pizza)
grid = QtWidgets.QGridLayout(self)
grid.addWidget(self.list_widget, 0, 0, 1, 2)
grid.addWidget(client_btn, 1, 0)
grid.addWidget(pizza_btn, 1, 1)
@QtCore.pyqtSlot()
def add_client(self):
client_items = self.list_widget.findItems(
"Pizza",
QtCore.Qt.MatchExactly
)
for item in reversed(client_items):
row = self.list_widget.row(item)
it = self.list_widget.takeItem(row)
del it
self.list_widget.addItem("Client")
@QtCore.pyqtSlot()
def add_pizza(self):
self.list_widget.addItem("Pizza")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())