QMap, index of entry without iterating through
-
wrote on 22 Oct 2021, 09:09 last edited by SPlatten
I have an instance of QMap, I need to get the index of an entry in the map. Of course I could iterate through the map until I find a match.
-
@J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
The other part of this problem:
https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-stringOnce I have the key I want to use it to construct a control name, but how do I find the control using a string?
wrote on 22 Oct 2021, 10:04 last edited by@SPlatten said in QMap, index of entry without iterating through:
o I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
I am not sure to understand what you want to do :(
A
QMap()
is an ordered list, so each time you add a new key, index of each key may change. IsQMap()
the right type for you need, perhapsQHash()
may be better? -
I have an instance of QMap, I need to get the index of an entry in the map. Of course I could iterate through the map until I find a match.
@SPlatten what do you mean with index ? the key ?
-
@SPlatten what do you mean with index ? the key ?
wrote on 22 Oct 2021, 09:22 last edited by@J-Hilk each entry in the map is keyed with the host name for a particular machine, I need to translate this into a position in the map, because when entries are added to the map controls on the display are also added using an index, so the position in he map is important so I can lookup the correct control to update.
-
@J-Hilk each entry in the map is keyed with the host name for a particular machine, I need to translate this into a position in the map, because when entries are added to the map controls on the display are also added using an index, so the position in he map is important so I can lookup the correct control to update.
@SPlatten If I understand you correctly, then keys() is still the function to call.
it should be ordered by the order of insertion into the QMap, except duplicated keys of course
-
@SPlatten If I understand you correctly, then keys() is still the function to call.
it should be ordered by the order of insertion into the QMap, except duplicated keys of course
wrote on 22 Oct 2021, 09:30 last edited by SPlatten@J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
The other part of this problem:
https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-stringOnce I have the key I want to use it to construct a control name, but how do I find the control using a string?
-
@J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
The other part of this problem:
https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-stringOnce I have the key I want to use it to construct a control name, but how do I find the control using a string?
myMap.keys().indexOf("someKey") ?
-
wrote on 22 Oct 2021, 09:34 last edited by
What if instead of QMap you'd go with QVector of QPair? Seems like more convenient for what you need.
-
@J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
The other part of this problem:
https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-stringOnce I have the key I want to use it to construct a control name, but how do I find the control using a string?
wrote on 22 Oct 2021, 10:04 last edited by@SPlatten said in QMap, index of entry without iterating through:
o I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
I am not sure to understand what you want to do :(
A
QMap()
is an ordered list, so each time you add a new key, index of each key may change. IsQMap()
the right type for you need, perhapsQHash()
may be better? -
@SPlatten said in QMap, index of entry without iterating through:
o I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.
I am not sure to understand what you want to do :(
A
QMap()
is an ordered list, so each time you add a new key, index of each key may change. IsQMap()
the right type for you need, perhapsQHash()
may be better?wrote on 22 Oct 2021, 10:06 last edited by SPlatten@KroMignon , thank you, will use QHasp.
-
@KroMignon , thank you, will use QHasp.
wrote on 22 Oct 2021, 10:08 last edited by@SPlatten said in QMap, index of entry without iterating through:
are you saying the index order can change with each new insertion into the map?
Yes I do (cf. https://doc.qt.io/qt-5/containers.html):
QMap<Key, T> This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn't matter QHash is a faster alternative.
-
@SPlatten If I understand you correctly, then keys() is still the function to call.
it should be ordered by the order of insertion into the QMap, except duplicated keys of course
wrote on 22 Oct 2021, 10:39 last edited by JonB@J-Hilk said in QMap, index of entry without iterating through:
@SPlatten If I understand you correctly, then keys() is still the function to call.
it should be ordered by the order of insertion into the QMap, except duplicated keys of courseFor the record, you should not say this.
QMap
is not ordered by insertion, it is ordered by keys. Andkeys()
(orvalues()
) comes out in ascending key order. Unless perchance you are talking about the ordering of the values in a multi-map, where within one key the values are ordered by insertion, but I don't think this is what you/OP meant.@SPlatten
Like others, I am confused about what you are storing in the map, and what you are using the values for. And again as others have said, if for some reason you care about an index number, you should not be using aQMap
.but how do I find the control using a string?
QObject::findChildren<optionalWidgetType>(optionalWidgetObjectName)
. Pick whatever is best to start searching down from as the ancestor widget you callfindChildren<>()
on. (findChild<>()
uses this to pick out just one.) -
@J-Hilk said in QMap, index of entry without iterating through:
@SPlatten If I understand you correctly, then keys() is still the function to call.
it should be ordered by the order of insertion into the QMap, except duplicated keys of courseFor the record, you should not say this.
QMap
is not ordered by insertion, it is ordered by keys. Andkeys()
(orvalues()
) comes out in ascending key order. Unless perchance you are talking about the ordering of the values in a multi-map, where within one key the values are ordered by insertion, but I don't think this is what you/OP meant.@SPlatten
Like others, I am confused about what you are storing in the map, and what you are using the values for. And again as others have said, if for some reason you care about an index number, you should not be using aQMap
.but how do I find the control using a string?
QObject::findChildren<optionalWidgetType>(optionalWidgetObjectName)
. Pick whatever is best to start searching down from as the ancestor widget you callfindChildren<>()
on. (findChild<>()
uses this to pick out just one.)wrote on 22 Oct 2021, 10:42 last edited by SPlatten@JonB , as suggested by @KroMignon I am now using QHash, I store the host name of remote systems int the map and use the index of there position to select controls that are in a display.
I'm now using findChild
-
@JonB , as suggested by @KroMignon I am now using QHash, I store the host name of remote systems int the map and use the index of there position to select controls that are in a display.
I'm now using findChild
wrote on 22 Oct 2021, 10:46 last edited by JonB@SPlatten said in QMap, index of entry without iterating through:
and use the index of there position
Not sure what you mean by this, though if it's working for you it doesn't matter. I think/assume you are storing some index number as the value against a key in a
QHash
. -
@J-Hilk said in QMap, index of entry without iterating through:
@SPlatten If I understand you correctly, then keys() is still the function to call.
it should be ordered by the order of insertion into the QMap, except duplicated keys of courseFor the record, you should not say this.
QMap
is not ordered by insertion, it is ordered by keys. Andkeys()
(orvalues()
) comes out in ascending key order. Unless perchance you are talking about the ordering of the values in a multi-map, where within one key the values are ordered by insertion, but I don't think this is what you/OP meant.@SPlatten
Like others, I am confused about what you are storing in the map, and what you are using the values for. And again as others have said, if for some reason you care about an index number, you should not be using aQMap
.but how do I find the control using a string?
QObject::findChildren<optionalWidgetType>(optionalWidgetObjectName)
. Pick whatever is best to start searching down from as the ancestor widget you callfindChildren<>()
on. (findChild<>()
uses this to pick out just one.)@JonB said in QMap, index of entry without iterating through:
For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys.
I never said, QMap us ordered by insertion!I said:
>keys() is still the function to call. it should be ordered by the order of insertion into the QMapit being herethe QList<Keys>
that keys() returns.
nope, was barking up the wrong tree. @JonB is right
-
@JonB said in QMap, index of entry without iterating through:
For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys.
I never said, QMap us ordered by insertion!I said:
>keys() is still the function to call. it should be ordered by the order of insertion into the QMapit being herethe QList<Keys>
that keys() returns.
nope, was barking up the wrong tree. @JonB is right
wrote on 22 Oct 2021, 10:50 last edited by JonB@J-Hilk
As I said, QList<Key> QMap::keys() const (and for that mattervalues()
too) is ordered by ascending key, not by ascending order of key insertion.Returns a list containing all the keys in the map in ascending order.
Where do you quote from which says anything about insertion order?
QMap
does not maintain any insertion order. -
@J-Hilk
As I said, QList<Key> QMap::keys() const (and for that mattervalues()
too) is ordered by ascending key, not by ascending order of key insertion.Returns a list containing all the keys in the map in ascending order.
Where do you quote from which says anything about insertion order?
QMap
does not maintain any insertion order.@JonB
I said, should.And because of the why the template is set up, it iterates over the map
1030 template <class Key, class T> 1031 Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const 1032 { 1033 QList<Key> res; 1034 res.reserve(size()); 1035 const_iterator i = begin(); 1036 while (i != end()) { 1037 res.append(i.key()); 1038 ++i; 1039 } 1040 return res; 1041 }
so as long as you don't have multiple values to one key,
except duplicated keys of course
then QMap should be a very linear tree and keys should return in our expected order.
Everything with caveats of course, this all breaks very easily :D
-
@JonB
I said, should.And because of the why the template is set up, it iterates over the map
1030 template <class Key, class T> 1031 Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const 1032 { 1033 QList<Key> res; 1034 res.reserve(size()); 1035 const_iterator i = begin(); 1036 while (i != end()) { 1037 res.append(i.key()); 1038 ++i; 1039 } 1040 return res; 1041 }
so as long as you don't have multiple values to one key,
except duplicated keys of course
then QMap should be a very linear tree and keys should return in our expected order.
Everything with caveats of course, this all breaks very easily :D
wrote on 22 Oct 2021, 11:05 last edited by@J-Hilk
I really do not understand what you are saying. Thatconst_iterator i = begin();
is iterating theQMap
in ascending key order, and so that is whatkeys()
returns, not insertion order. I have taken this to a PM post to you to clarify, rather than hijacking this thread. -
Disregard almost everything I said 🙈,
of course the internal storage is sorted on key/value pair insert
1/18