PDA

View Full Version : Copy a QMap



ggdev001
12th February 2013, 11:46
Hello, I am having problems copying one QMap to another, any help would be appreciated.

This is my code (where the copying should occur):

PS. I have solved the issue, seems I just had to add i++ to the code below; Just one question, it seems
the data is added in the reverse order to the Qmap. For example, when I added "param1","test1"
key value pair to the QMap as in the end of this post, and also "param2", "test2" ... they are read in an reverse
order in the loop below. Is it supposed to be like this?? I want the data to be added in the
same order as I added in the code in the end.. Thanks.


bool MyNetworkClass::initializeRequestParam(QMap<QString, QString> params)
{
QMap<QString, QString>::const_iterator i = params.constBegin();
while (i != params.constEnd()) {
requestParameters.insert(i.key(), i.value());
i++; // added
}

return true; // this is default...
}

where
requestParameters is member variable:
QMap<QString, QString> requestParameters;

Now, when I try to issue the above function:


MyNetworkClass *p = new MyNetworkClass();


QMap<QString, QString> params;

params.insert("param1", "test1");
params.insert("param2", "test2");
params.insert("param3", "test3");

p->initializeRequestParam(params);

The last line "initializeRequestParam" - seems to not work.. I think it gets in an endless loop...
when I debugged into it, I think it managed to successfully copy only one "param1", "test1" pair to the
reqestParameters destination...
Any help greatly appreciated as to how to make this function initializeRequestParam work?? and copy one QMap into another..???

wysota
12th February 2013, 12:15
You are not incrementing the iterator in your function anywhere.

Zlatomir
12th February 2013, 12:17
Also QMap has a copy constructor, see here (http://qt-project.org/doc/qt-4.8/qmap.html#QMap-2).

ggdev001
12th February 2013, 13:12
Yes, I noticed it thanks. Could you please look at my modified question?? i.e., the P.S. part?? Thank you.

wysota
12th February 2013, 13:41
QMap is sorted by key order.