# Import the necessary library from PyQt5.QtWidgets import QComboBox # Create a QComboBox widget combo_box = QComboBox() # Add items to the QComboBox combo_box.addItem("Apple", "fruit") combo_box.addItem("Carrot", "vegetable") combo_box.addItem("Fish", "protein") # Select an item in the QComboBox based on its data value index = combo_box.findData("vegetable") combo_box.setCurrentIndex(index)
# Import the necessary library from PyQt5.QtWidgets import QComboBox # Create a QComboBox widget combo_box = QComboBox() # Add items to the QComboBox combo_box.addItem("Apple", "fruit") combo_box.addItem("Carrot", "vegetable") combo_box.addItem("Fish", "protein") # Retrieve the data value of the selected item in the QComboBox data = combo_box.itemData(combo_box.currentIndex()) print(data)Package library: PyQt5.QtWidgets.