[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
-
do you have a suggestion how i can solve my problem ?
-
@qDebug() << ui->cb_test->itemData(index);@
-
the output of the above is:
@QVariant(int, 0)
QVariant(int, 2)
QVariant(int, 3) @I want to use this combobox to set QSerialPort parity:
@myQSerielPort->setParity(ui->cb_test->itemData(index); @
-
And that's the correct output.
If you want to retrieve the value you have to convert the QVariant back to your enum
-
Ahhhhh :), OK thank you
-
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 :)