[solved] Combobox UserRole ?
-
How can i get the enum values of Parity ?
I expect to get 0, 2 and 3
Problem is, i do not understand the USerrole stuff.@ ui->cb_test->addItem("NO", QSerialPort::NoParity);
ui->cb_test->addItem("EVEN", QSerialPort::EvenParity);
ui->cb_test->addItem("ODD", QSerialPort::OddParity);@@void MainWindow::on_cb_test_currentIndexChanged(int index)
{
qDebug() << index;
qDebug() << ui->cb_test->itemData(index, 0);
}@thank you
-
Hi,
@qDebug() << ui->cb_test->itemData(index, 0);@
You are using the wrong role here. By default itemData gets the data from Qt::UserRole so what you put in using addItem. Here you are requesting the data from Qt::DisplayRole
-
And that's the correct output.
If you want to retrieve the value you have to convert the QVariant back to your enum
-
You're welcome,
Since you have everything working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)