[Qt-interest] Select index of comboBox with item Data (ID)

Malyushytsky, Alex alex at wai.com
Mon Jun 28 22:52:25 CEST 2010


Whatever you call it item data is just data associated with item in combobox.

To support operation you are looking for ("->selectItem(variantID);"  )
it would have to either  call " myCombo->findData(variantID)" itself or
keep map (extra memory/time to keep it updated) which will provide quick way to find item data by index.

Second approach will slow down the normal usage and increase memory requirements.
So I think reasons it is not done by default are clear.

In the most cases findData will be fast enough unless you call it too often, in this case you might want to reconsider your design.
First I would recommend to check if using findData gives you any visible delay.

If you really need very fast access to item by data
create QMap or QHash  with data as a key and index as value.

Regards,
   Alex



-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Matthias Pospiech
Sent: Sunday, June 27, 2010 1:40 PM
To: Qt Interest
Subject: [Qt-interest] Select index of comboBox with item Data (ID)

I can use the userData of a comboBox to save IDs together with the display text.
I can set these IDs using either

myCombo->setItemData(i, variantId);

or
myCombo->addItem("Text", variantId);

I can get the ID for a known index using

QVariant variantId = myCombo->itemData(myCombo->getCurrentIndex());

But how do I select the index using an ID?
Is this the only option?

myCombo->setCurrentIndex(myCombo->findData(variantID));

I am asking because I would have expected something like

myCombo->selectItem(variantID);

Matthias



_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”




More information about the Qt-interest-old mailing list