QMap iteration gets inexisting item
-
When I iterate a QMap for the second time I get an inexisting item why?
typedef struct { uint8_t connected; QByteArray address; uint8_t type; uint8_t enabled; } item_t; QMap<uint8_t, item_t> map; void Foo( QByteArray address, uint8_t connectionState, uint8_t itemType, uint8_t id ) { if ( map.contains( id ) == false ) { map.insert( id, { .connected = connectionState, .address = address, .type = itemType, .enabled = 0 } ); } else { map[ id ].connected = connectionState; } foreach ( const uint8_t &key, map.keys() ) { qDebug() << "id" << key << "-" << map[ key ].address.toHex( ':' ) << "- connectionState" << map[ key ].connected; } }If i run
Foo( hereByteArray, 1, 1, 1 )the output isid 1 - "28:9e:39:68:72:e8" - connectionState 1as expected. But If I continue to run
Foo( hereByteArray, 0/1, 1, 1 )the output becomeid 0 - "" - connectionState 0 id 1 - "28:9e:39:68:72:e8" - connectionState 0even if I never insert the item "0". Why ?
-
When I iterate a QMap for the second time I get an inexisting item why?
typedef struct { uint8_t connected; QByteArray address; uint8_t type; uint8_t enabled; } item_t; QMap<uint8_t, item_t> map; void Foo( QByteArray address, uint8_t connectionState, uint8_t itemType, uint8_t id ) { if ( map.contains( id ) == false ) { map.insert( id, { .connected = connectionState, .address = address, .type = itemType, .enabled = 0 } ); } else { map[ id ].connected = connectionState; } foreach ( const uint8_t &key, map.keys() ) { qDebug() << "id" << key << "-" << map[ key ].address.toHex( ':' ) << "- connectionState" << map[ key ].connected; } }If i run
Foo( hereByteArray, 1, 1, 1 )the output isid 1 - "28:9e:39:68:72:e8" - connectionState 1as expected. But If I continue to run
Foo( hereByteArray, 0/1, 1, 1 )the output becomeid 0 - "" - connectionState 0 id 1 - "28:9e:39:68:72:e8" - connectionState 0even if I never insert the item "0". Why ?
@Andrew23 said in QMap iteration gets inexitent item:
But If I continue to run Foo( hereByteArray, 0/1, 1, 1 ) the output become
id 0 - "" - connectionState 0
id 1 - "28:9e:39:68:72:e8" - connectionState 0even if I never insert the item "0". Why ?
At first glance, I would say you fell for the []-operator "mistake".
Try to avoid [] everywhere when dealing withQMaps.Because if you try to look-up a key-value pair with [] and the key is not found, it will construct an empty, default key value pair at this position.
I could be wrong but this is what your
[0]entry seems to be.
Replace every access to the map with.value(key)and it should not happen anymoreSo instead of
map[ key ].address.toHex( ':' )better (and everywhere else)
map.value(key).address.toHex( ':' ) -
When I iterate a QMap for the second time I get an inexisting item why?
typedef struct { uint8_t connected; QByteArray address; uint8_t type; uint8_t enabled; } item_t; QMap<uint8_t, item_t> map; void Foo( QByteArray address, uint8_t connectionState, uint8_t itemType, uint8_t id ) { if ( map.contains( id ) == false ) { map.insert( id, { .connected = connectionState, .address = address, .type = itemType, .enabled = 0 } ); } else { map[ id ].connected = connectionState; } foreach ( const uint8_t &key, map.keys() ) { qDebug() << "id" << key << "-" << map[ key ].address.toHex( ':' ) << "- connectionState" << map[ key ].connected; } }If i run
Foo( hereByteArray, 1, 1, 1 )the output isid 1 - "28:9e:39:68:72:e8" - connectionState 1as expected. But If I continue to run
Foo( hereByteArray, 0/1, 1, 1 )the output becomeid 0 - "" - connectionState 0 id 1 - "28:9e:39:68:72:e8" - connectionState 0even if I never insert the item "0". Why ?
@Andrew23 To add to @Pl45m4 this behavior is explained in the documentation which you should read: https://doc.qt.io/qt-6/qmap.html#operator-5b-5d
-
@Andrew23 said in QMap iteration gets inexitent item:
But If I continue to run Foo( hereByteArray, 0/1, 1, 1 ) the output become
id 0 - "" - connectionState 0
id 1 - "28:9e:39:68:72:e8" - connectionState 0even if I never insert the item "0". Why ?
At first glance, I would say you fell for the []-operator "mistake".
Try to avoid [] everywhere when dealing withQMaps.Because if you try to look-up a key-value pair with [] and the key is not found, it will construct an empty, default key value pair at this position.
I could be wrong but this is what your
[0]entry seems to be.
Replace every access to the map with.value(key)and it should not happen anymoreSo instead of
map[ key ].address.toHex( ':' )better (and everywhere else)
map.value(key).address.toHex( ':' ) -
@Andrew23 said in QMap iteration gets inexisting item:
it's ok to use the [] operator to write the map item value?
Yes, of course, since here you are writing to and/or creating the element, you don't have to worry about it auto-creating a "blank" one. What you should not do is read from
map[...], e.g.if (map[...]) ...orqDebug() << map[...]since those would create the entry if it does not exist. -
@Andrew23 said in QMap iteration gets inexisting item:
it's ok to use the [] operator to write the map item value?
Yes, of course, since here you are writing to and/or creating the element, you don't have to worry about it auto-creating a "blank" one. What you should not do is read from
map[...], e.g.if (map[...]) ...orqDebug() << map[...]since those would create the entry if it does not exist. -
A Andrew23 has marked this topic as solved on