QComboBox comboBox; comboBox.addItem("Option 1", 1); comboBox.addItem("Option 2", 2); comboBox.addItem("Option 3", 3); int index = comboBox.findData(2); // returns 1
QComboBox comboBox; comboBox.addItem("Option 1", "Value 1"); comboBox.addItem("Option 2", "Value 2"); comboBox.addItem("Option 3", "Value 3"); QString value = comboBox.itemData(comboBox.findText("Option 2")).toString(); // value now contains "Value 2"In this example, we create a QComboBox and add three items to its list, each with a custom data value. We then call findText to search for the index of the item with text "Option 2". We then use the itemData method to retrieve the custom data value for the item at that index. These examples use the Qt package library, which is a C++ toolkit for developing cross-platform applications.