# Import necessary module from PyQt5.QtWidgets import QListWidget, QListWidgetItem # Create a QListWidget myListWidget = QListWidget() # Add some QListWidgetItem objects to the QListWidget item1 = QListWidgetItem("Item 1") item2 = QListWidgetItem("Item 2") item3 = QListWidgetItem("Item 3") myListWidget.addItem(item1) myListWidget.addItem(item2) myListWidget.addItem(item3) # Use findItems() to search for items that contain "2" matchingItems = myListWidget.findItems("2", Qt.MatchContains) # Print the text of the matching items for item in matchingItems: print(item.text()) # Output: "Item 2"
# Import necessary module from PyQt5.QtWidgets import QListWidget, QListWidgetItem # Create a QListWidget myListWidget = QListWidget() # Add some QListWidgetItem objects to the QListWidget item1 = QListWidgetItem("Item 1") item2 = QListWidgetItem("Item 2") item3 = QListWidgetItem("Item 3") myListWidget.addItem(item1) myListWidget.addItem(item2) myListWidget.addItem(item3) # Use findItems() to search for items that exactly match "Item 2" matchingItems = myListWidget.findItems("Item 2", Qt.MatchExactly) # Remove the matching item from the QListWidget if matchingItems: index = myListWidget.row(matchingItems[0]) myListWidget.takeItem(index)In this example, we create a QListWidget and add three QListWidgetItem objects to it. We then use the findItems() method to search for items that exactly match the string "Item 2", which should return a list containing the item with text "Item 2". We then remove the matching item from the QListWidget using the takeItem() method. The package library used in these examples is PyQt5, a set of Python bindings for the Qt application framework.