QComboBox* comboBox = new QComboBox; comboBox->addItem("Item 1", QVariant(10)); comboBox->addItem("Item 2", QVariant(20)); comboBox->addItem("Item 3", QVariant(30));
QVariant userData = comboBox->itemData(comboBox->currentIndex()); int value = userData.toInt();In this example, we are getting the user data of the currently selected item from the QComboBox. We are then converting the user data to an integer using the toInt() function. Package Library: Qt C++ Library