January 11, 2012

kanakas kanakas
Lab Rat
28 posts

how can i get an unsorted qmap?

 

Is it possible to set up a QMap that returns the keys in the order in which they were added?
I have created my whole program using a qmap and i would prefer not to change the qmap with sth else.

6 replies

January 11, 2012

peppe peppe
Ant Farmer
1026 posts

No. QMap is sorted by the key. You could use a QHash + a QList or something like that.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

January 11, 2012

KA51O KA51O
Robot Herder
478 posts

I suggest you read this [developer.qt.nokia.com] so you get an understanding of what sort of container classes exist and what their individual characteristics are.

If you want “first in first out” including a Key -> Value entry I’d suggest you use a QQueue holding QPairs

  1. QQueue<QPair<Key,Value>>

This way you will loose the benefit of QMap which is quick acccess to elements via their key, but you gain the advantage of quicker insertion and deletion of elements.

January 23, 2012

kanakas kanakas
Lab Rat
28 posts

Hi,

I changed my code and I created a qlist with qmap, but I cant find a way to check the key of my qmap. I have a qlist qmap with some data e.g.(id,1),(test_id, “a”) and I use the code that follows to write some data to a file. But I would like to write different data if the key of the map is “id” and different if it is “test_id”. How can I check the key?

  1.  QMultiMap<QString, QString>::iterator lab_id = examinations.find("lab_id");
  2.     QMultiMap<QString, QString>::iterator test_id = examinations.find("test_id");
  3.        
  4.          while(!chosen_examinations2.isEmpty()) {
  5.               QMap<QString,QString> exam = chosen_examinations2.takeFirst();
  6.  
  7.             if (exam.key()=="id"){ //**here how can i check this?? , i get an error
  8.             xmlWriter->writeStartElement("patient_id");
  9.             xmlWriter->writeCharacters (exam["id"]);
  10.             xmlWriter->writeEndElement();
  11.             xmlWriter->writeStartElement("patient_exams");
  12.             }
  13.             else{
  14.             while (exam["test_id"]!=test_id.value())
  15.                { test_id++;
  16.                    lab_id++;
  17.                    specimen++;}
  18.  
  19.             xmlWriter->writeStartElement("lab_test");
  20.  
  21.             xmlWriter->writeStartElement("lab_id");
  22.             xmlWriter->writeCharacters(lab_id.value());
  23.             xmlWriter->writeEndElement();
  24.  
  25.             xmlWriter->writeStartElement("test_id");
  26.             xmlWriter->writeCharacters (test_id.value());
  27.             xmlWriter->writeEndElement();
  28.             xmlWriter->writeEndElement();
  29.             xmlWriter->writeEndElement();
  30.             }

January 23, 2012

koahnig koahnig
Area 51 Engineer
2879 posts

I do not think that your approach is a good idea.

You use a map and you are not satisfied, because of sorting through the map. Wrapping the QMap with QList just puts the whole as is into another container, but it will not change the situation regarding the sorting. It gets only complicated.

You need to substitute QMap with QList. Unfortunately, you have to carry all the pain in changing your source. There is no easy escape route.

January 23, 2012

KA51O KA51O
Robot Herder
478 posts

Again having a QList of QMaps doesn’t make sense for your use case.

One way to solve your problem without you having to remove your old QMap stuff might be to store a QQueue additionally. The QQueue just holds the keys for the QMap values in the order in which they were inserted. This way you store the order in which the values were entered into the map in the QQueue and can access the values inside the QMap using this keys from the QQueue.

BUT for this to work you need to change the keys you use for your QMap to something unique (i.e. NOT “id” or “test_id”). The id and test_id stuff could be stored as a sort of property parameter of the value you store in the map.

January 25, 2012

kanakas kanakas
Lab Rat
28 posts

thanks. solved

 
  ‹‹ [Beginner]Calculate two values from lineEdit      Send Signals to a specified receiver ››

You must log in to post a reply. Not a member yet? Register here!