QComboBox* comboBox = new QComboBox(); comboBox->addItem("Item 1"); comboBox->addItem("Item 2"); comboBox->setCurrentIndex(1);
QStringList items = {"Apple", "Banana", "Cherry", "Durian"}; QComboBox* comboBox = new QComboBox(); comboBox->addItems(items); comboBox->setCurrentIndex(items.indexOf("Cherry"));This code creates a new QComboBox, adds several items to its list using addItems, and selects the "Cherry" item using setCurrentIndex and indexOf, which returns the index of the given string in the list. The QComboBox class and its setCurrentIndex method are part of the Qt GUI library, which is included in the Qt framework for C++ development.