[Qt-interest] Select index of comboBox with item Data (ID)
Malyushytsky, Alex
alex at wai.com
Wed Jun 30 02:41:24 CEST 2010
>> but would help developers avoid writing the same code every time they want to select an item with specific data.
What you are going to save?
Typing few characters:
combo->setCurrentIndex( combo->findData(data, role, flags) );
vs
combo->selectItem( data, role, flags );
>> wouldn't hurt Qt,
As for me it would hurt Qt by adding to interface unnecessary function.
Also you will have to check the source code or documentation to understand the cost of such function.
As is, the cost is clear and I don't have to read the source to understand complexity involved.
If it was my choice I would keep my interface as simple and short as possible.
But nobody prevents you from writing a function to do thing you want.
You basically wrote it already.
Again I would not make it a class member of QComboBox cause
- you don't access anything protected or private,
- you function may maintain some external for combobox data ( for example QHash ) and if you don't,
you basically have no reason even to create it, code written by you would work just fine.
Finally I am not a developer of Qt as most of others on this forum.
And Qt developers might not see things the way I do.
So you can provide suggestion/request of the feature you want.
I believe it is done at:
http://bugreports.qt.nokia.com/secure/Dashboard.jspa
Alex
-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Constantin Makshin
Sent: Tuesday, June 29, 2010 3:01 PM
To: Qt Interest
Subject: Re: [Qt-interest] Select index of comboBox with item Data (ID)
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
_______________________________________________
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