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. QSettings::registerFormat change from QMap to QHash

QSettings::registerFormat change from QMap to QHash

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.2k Views 3 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.
  • Dave WattsD Offline
    Dave WattsD Offline
    Dave Watts
    wrote on last edited by
    #1

    Hi

    I am using the QSettings class to save XML content, it looks like this

    bool readSettingsXml(QIODevice &device, QMap<QString, QVariant> &map);
    bool writeSettingsXml(QIODevice &device, const QMap<QString, QVariant> &map);
    
    static const QSettings::Format xmlFormat = QSettings::registerFormat("xml", &readSettingsXml, &writeSettingsXml);
    

    At the moment QMap is used, but QMap sorts the entries and therefore everything is out of order. I could use QHash instead, so I tried changing to QHash but then the code wouldn't compile since the readSettingsXml & writeSettingsXml functions weren't correct. Is there anyway to use a QHash to store the settings OR is there anyway to create a QMap that doesn't result in sorted keys?

    Thanks

    Dave

    Dave WattsD 1 Reply Last reply
    0
    • Dave WattsD Dave Watts

      Hi

      I am using the QSettings class to save XML content, it looks like this

      bool readSettingsXml(QIODevice &device, QMap<QString, QVariant> &map);
      bool writeSettingsXml(QIODevice &device, const QMap<QString, QVariant> &map);
      
      static const QSettings::Format xmlFormat = QSettings::registerFormat("xml", &readSettingsXml, &writeSettingsXml);
      

      At the moment QMap is used, but QMap sorts the entries and therefore everything is out of order. I could use QHash instead, so I tried changing to QHash but then the code wouldn't compile since the readSettingsXml & writeSettingsXml functions weren't correct. Is there anyway to use a QHash to store the settings OR is there anyway to create a QMap that doesn't result in sorted keys?

      Thanks

      Dave

      Dave WattsD Offline
      Dave WattsD Offline
      Dave Watts
      wrote on last edited by
      #2

      @Dave-Watts

      One Idea I have is to prefix the key with a number like

       mymap["1_mykey"] = "hi";
       mymap["2_mykey"] = "there";
      

      then I would have to strip out the number and _ prior to writing the XML tag. A bit drastic, anyone with anything better?

      raven-worxR 1 Reply Last reply
      0
      • Dave WattsD Dave Watts

        @Dave-Watts

        One Idea I have is to prefix the key with a number like

         mymap["1_mykey"] = "hi";
         mymap["2_mykey"] = "there";
        

        then I would have to strip out the number and _ prior to writing the XML tag. A bit drastic, anyone with anything better?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @Dave-Watts
        Since this is the nature of QMap per definition (for a reason) the workaround you've posted is the only possibility. But keep in mind not to simply sort the strings, but extract the numbers first and then do the sorting upon the integer values. Or you will result in "1", "11", "2", ... for example.

        Btw what makes you think that QHash keeps the order?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        Dave WattsD 1 Reply Last reply
        2
        • raven-worxR raven-worx

          @Dave-Watts
          Since this is the nature of QMap per definition (for a reason) the workaround you've posted is the only possibility. But keep in mind not to simply sort the strings, but extract the numbers first and then do the sorting upon the integer values. Or you will result in "1", "11", "2", ... for example.

          Btw what makes you think that QHash keeps the order?

          Dave WattsD Offline
          Dave WattsD Offline
          Dave Watts
          wrote on last edited by Dave Watts
          #4

          @raven-worx said in QSettings::registerFormat change from QMap to QHash:

          QHash

          "When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered."

          http://doc.qt.io/qt-5/qhash.html

          Maybe I will just use letters instead of numbers

          A_dataXYZ
          B_dataABC

          raven-worxR 1 Reply Last reply
          0
          • Dave WattsD Dave Watts

            @raven-worx said in QSettings::registerFormat change from QMap to QHash:

            QHash

            "When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered."

            http://doc.qt.io/qt-5/qhash.html

            Maybe I will just use letters instead of numbers

            A_dataXYZ
            B_dataABC

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @Dave-Watts said in QSettings::registerFormat change from QMap to QHash:

            "When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered."

            yes, and how does this match up with the following you said?! o.O

            @Dave-Watts said in QSettings::registerFormat change from QMap to QHash:

            At the moment QMap is used, but QMap sorts the entries and therefore everything is out of order. I could use QHash instead

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            Dave WattsD 1 Reply Last reply
            0
            • raven-worxR raven-worx

              @Dave-Watts said in QSettings::registerFormat change from QMap to QHash:

              "When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered."

              yes, and how does this match up with the following you said?! o.O

              @Dave-Watts said in QSettings::registerFormat change from QMap to QHash:

              At the moment QMap is used, but QMap sorts the entries and therefore everything is out of order. I could use QHash instead

              Dave WattsD Offline
              Dave WattsD Offline
              Dave Watts
              wrote on last edited by
              #6

              @raven-worx

              QMap sorts your items, i.e. it mixes up the original order, which could be unsorted

              QHash does not sort them (ie. "arbitarily ordered") though why QT choose these words who knows, its not very clear!

              S kshegunovK 2 Replies Last reply
              0
              • Dave WattsD Dave Watts

                @raven-worx

                QMap sorts your items, i.e. it mixes up the original order, which could be unsorted

                QHash does not sort them (ie. "arbitarily ordered") though why QT choose these words who knows, its not very clear!

                S Offline
                S Offline
                Stoyan
                wrote on last edited by Stoyan
                #7

                @Dave-Watts
                I don't think that "arbitrarily ordered" means that keys will stay in order of addition.
                I don't understand why you want the keys to be sorted in some specific order. QSettings, QMaps and XML are all based on key-value principle and don't depend on any order.
                But, if you still want to order keys somehow, I guess that this have to be done in the functions readSettingsXml() and writeSettingsXml(). Perhaps with using of groups.

                1 Reply Last reply
                2
                • Dave WattsD Dave Watts

                  @raven-worx

                  QMap sorts your items, i.e. it mixes up the original order, which could be unsorted

                  QHash does not sort them (ie. "arbitarily ordered") though why QT choose these words who knows, its not very clear!

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @Dave-Watts said in QSettings::registerFormat change from QMap to QHash:

                  QHash does not sort them (ie. "arbitarily ordered") though why QT choose these words who knows, its not very clear!

                  It's quite clear, they are arbitrarily ordered. QHash is an unordered container, you are not to rely on specific order of appearance neither of keys, nor of values.

                  @Stoyan said in QSettings::registerFormat change from QMap to QHash:

                  I don't think that "arbitrarily ordered" means that keys will stay in order of addition.

                  They won't, not in the general case. The order of keys is implementation specific - what variation of the hash table was chosen (I think Qt uses skip-list).

                  PS. Nope, it uses a regular linked list.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  3

                  • Login

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