[Qt-interest] Select index of comboBox with item Data (ID)
Constantin Makshin
cmakshin at gmail.com
Wed Jun 30 00:00:39 CEST 2010
IMHO, something like inline function
void QComboBox::selectItem (const QVariant& data, int role = Qt::UserRole, Qt::MatchFlags flags flags = Qt::MatchExactly | Qt::MatchCaseSensitive)
{
setCurrentIndex(findData(data, role, flags));
}
wouldn't hurt Qt, but would help developers avoid writing the same code every time they want to select an item with specific data.
On Tuesday 29 June 2010 00:52:25 Malyushytsky, Alex wrote:
> 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
More information about the Qt-interest-old
mailing list