PDA

View Full Version : QComboBox userData always returns invalid



_aXe_
30th September 2013, 13:10
Hi,

i try to use a ComboBox for a selection of Baudrates.
I wan't to Display the Label for a Baudrate and store an unisigned int in the userData.

Here's my Code:


typedef struct
{
QString m_displayName;
unsigned int m_Btr0Btr1;
} baudrate_t;

...

const baudrate_t available_bauds[]={ {"1 MBaud/s",0x014},
{"800 kBaud/s",0x0016},
{"500 kBaud/s",0x001C},
{"250 kBaud/s",0x011C},
{"125 kBaud/s",0x031C},
{"100 kBaud/s",0x432F},
{"95 kBaud/s",0xC34E},
{"83 kBaud/s",0x852B},
{"50 kBaud/s",0x472F},
{"47 kBaud/s",0x1414},
{"33 kBaud/s",0x8B2F},
{"20 kBaud/s",0x532F},
{"10 kBaud/s",0x672F},
{"5 kBaud/s",0x7F7F},
};

...

for (int i = 0; i<( sizeof(available_bauds) / sizeof(available_bauds[0]) ); ++i)
{
QString Label = available_bauds[i].m_displayName;
unsigned int BTR0BTR1 = available_bauds[i].m_Btr0Btr1;
ui->cB_baudrate->addItem(Label,QVariant(BTR0BTR1));
}

/*DEBUG Code*/
qDebug() << ui->cB_baudrate->itemText(2);
QVariant check_data = ui->cB_baudrate->itemData(2,Qt::UserRole);
bool ok = false;
unsigned int check_btr0btr1 = check_data.toUInt(&ok);
if (ok)
{
qDebug() << check_btr0btr1;
}
/*END DEBUG CODE*/



the returned QVariant is always invalid.

What am i doing wrong?

OS Windows 7 64, QT 5.1.1, mingw48_32

ChrisW67
30th September 2013, 22:36
What am i doing wrong?

Apart from using confused units*... nothing I can see. It works fine here with Qt4.8.4 and Qt5.1.1 on Linux, and Qt 5.1.0 MingW on Windows.

Of course, if that debug code runs before the combo box is loaded...


* Either baud or bits per second, but not baud/sec. Baud and bits per second are not strictly the same thing either.

_aXe_
1st October 2013, 09:35
confused units*
You're right... shame on me :D

I was running the for Loop for the Data fill in the Construktor of the Dialog (after setupUi()) and the Debug Code in a slot called by a Button click (the Displayed comboBox looks good and the qDebug for the text delivers me the right entry)

Tried it with 5.1.0 and 5.0.2, same Problem, now I will try it in a complete new project, with nothing but the combobox