简体   繁体   中英

Qt QMap.insert() failing

I have a levelObjects object that's a QList of QVariants of QMaps:

QList< QVariant > levelObjects;

Later on, when I try to change the value of something in one of the QMaps, it doesn't seem to do anything:

qDebug() << "Before - " << levelObjects[id].toMap().value("x").toFloat() << ", newX = " << posX;
qDebug() << levelObjects[id].toMap();
QString stringNum = QString::number(posX);
levelObjects[id].toMap().insert("x", stringNum);
qDebug() << "After - " << levelObjects[id].toMap().value("x").toFloat();

produces:

Before -  207 , newX =  209.665 
QMap(("frame_name", QVariant(QString, "bumper.png") ) ( "height" ,  QVariant(QString, "25") ) ( "name" ,  QVariant(QString, "Bumper") ) ( "power" ,  QVariant(QString, "70") ) ( "rotation" ,  QVariant(QString, "0") ) ( "type" ,  QVariant(QString, "Bumper") ) ( "width" ,  QVariant(QString, "25") ) ( "x" ,  QVariant(QString, "207") ) ( "y" ,  QVariant(QString, "349") ) )  
After -  207 

Stepping through the offending line of code (the one with the .insert() ), I can see that it goes through a bunch of assembly subroutines related to QVariants, then goes through QMap::insert(), then some more assembly. My initial thought was that this might have been triggering some sort of signal which results in code elsewhere being executed, but that doesn't appear to be the case.

Any thoughts?

Edit: I solved the problem through a workaround - getting rid of the intermediate QVariant and having levelObjects be a QList of QMaps. However, I'm still curious if anyone has any insight as to why this was happening.

On the line:

levelObjects[id].toMap().insert("x", stringNum)

levelObjects[id].toMap() is returning a QMap object, and it is in the returned QMap object (not the original QVariant in the levelObjects list) that the insertion is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM