Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i get an unsorted qmap?
Forum Updated to NodeBB v4.3 + New Features

How can i get an unsorted qmap?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 13.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kanakas
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by
        #3

        I suggest you read "this":http://developer.qt.nokia.com/doc/qt-4.8/containers.html 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

        @
        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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kanakas
          wrote on last edited by
          #4

          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?

          @ QMultiMap<QString, QString>::iterator lab_id = examinations.find("lab_id");
          QMultiMap<QString, QString>::iterator test_id = examinations.find("test_id");

               while(!chosen_examinations2.isEmpty()) {
                    QMap<QString,QString> exam = chosen_examinations2.takeFirst();
          
                  if (exam.key()=="id"){ //**here how can i check this?? , i get an error
                  xmlWriter->writeStartElement("patient_id");
                  xmlWriter->writeCharacters (exam["id"]);
                  xmlWriter->writeEndElement();
                  xmlWriter->writeStartElement("patient_exams");
                  }
                  else{
                  while (exam["test_id"]!=test_id.value())
                     { test_id++;
                         lab_id++;
                         specimen++;}
          
                  xmlWriter->writeStartElement("lab_test");
          
                  xmlWriter->writeStartElement("lab_id");
                  xmlWriter->writeCharacters(lab_id.value());
                  xmlWriter->writeEndElement();
          
                  xmlWriter->writeStartElement("test_id");
                  xmlWriter->writeCharacters (test_id.value());
                  xmlWriter->writeEndElement();
                  xmlWriter->writeEndElement();
                  xmlWriter->writeEndElement();
                  }@
          
          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            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.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kanakas
                wrote on last edited by
                #7

                thanks. solved

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved