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. Incorrect order of xml attributes-Qt 5.6.2
Forum Update on Monday, May 27th 2025

Incorrect order of xml attributes-Qt 5.6.2

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 3.2k Views
  • 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.
  • M Offline
    M Offline
    Mallikarjun
    wrote on last edited by
    #1

    Hello All,
    As I explored, because of QHash randomisation I am getting random ordered xml attributes.In order to fix it(to get deterministic order) I followed some suggestions by locking the hash seed value by the following code before my Qt application starts.
    extern Q_CORE_EXPORT QBasicAtomicInt qt_qhash_seed;
    qt_qhash_seed.store(0);

    But still I am getting random ordered xml attributes only.Does anyone have clue on this?
    Thanks in advance.

    jsulmJ 1 Reply Last reply
    0
    • M Mallikarjun

      Hello All,
      As I explored, because of QHash randomisation I am getting random ordered xml attributes.In order to fix it(to get deterministic order) I followed some suggestions by locking the hash seed value by the following code before my Qt application starts.
      extern Q_CORE_EXPORT QBasicAtomicInt qt_qhash_seed;
      qt_qhash_seed.store(0);

      But still I am getting random ordered xml attributes only.Does anyone have clue on this?
      Thanks in advance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mallikarjun Why not just use QMap instead?
      From the documentation:
      "When iterating over a QMap, the items are always sorted by key"

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        QHash is not supposed to keep an ordered list. it is equivalent in functionality to std::unordered_map. if you want order use QMap

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mallikarjun
          wrote on last edited by
          #4

          @jsulm ,@VRonin I am not using QHash explicitly. I am using Qt's XML framework which internally uses QHash..

          From the document :http://doc.qt.io/qt-5/qhash.html#qGlobalQHashSeed
          "This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED. The contents of that variable, interpreted as a decimal value, will be used as the seed for qHash(). Alternatively, you can call the qSetGlobalQHashSeed() function."

          I tried setting the global hash seed using the code qSetGlobalQHashSeed(42); but still my xml attributes are not in order.

          VRoninV 1 Reply Last reply
          0
          • m.sueM Offline
            m.sueM Offline
            m.sue
            wrote on last edited by
            #5

            Hi,
            Please show the code where you call the XML classes. Maybe you can use another class instead that does not show the problem, e.g. XML Stream Iterator.
            -Michael.

            1 Reply Last reply
            0
            • M Mallikarjun

              @jsulm ,@VRonin I am not using QHash explicitly. I am using Qt's XML framework which internally uses QHash..

              From the document :http://doc.qt.io/qt-5/qhash.html#qGlobalQHashSeed
              "This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED. The contents of that variable, interpreted as a decimal value, will be used as the seed for qHash(). Alternatively, you can call the qSetGlobalQHashSeed() function."

              I tried setting the global hash seed using the code qSetGlobalQHashSeed(42); but still my xml attributes are not in order.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @Mallikarjun said in Incorrect order of xml attributes-Qt 5.6.2:

              temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization

              You misread the documentation. this does not mean it will be ordered. This ensures that if you run the application 2 times the elements will still be shuffled but in the same way i.e. it will be shuffled always in the same way but still shuffled.

              I stand by:

              programs should never depend on a particular QHash ordering

              Why do you need ordered attributes?!

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              M 1 Reply Last reply
              3
              • M Offline
                M Offline
                Mallikarjun
                wrote on last edited by
                #7

                @m-sue Below is my code :

                int main(int argc, char *argv[])
                {

                QCoreApplication a(argc, argv);
                
                *// Create a document to write XML*
                QDomDocument document;
                *// Making the root element*
                QDomElement root = document.createElement("address");
                root.setAttribute("door",12345);
                root.setAttribute("name", QString());
                root.setAttribute("pin", 54321);
                
                *// Adding the root element to the docuemnt*
                document.appendChild(root);
                *// Writing to a file*
                QFile file("C:/temp/myXLM.xml");
                if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
                {
                    qDebug() << "Open the file for writing failed";
                }
                else
                {
                    QTextStream stream(&file);
                    stream << document.toString();
                    file.close();
                    qDebug() << "Writing is done";
                }
                return a.exec();
                

                }

                I am expecting the output like this :<address door="12345" name="" pin="54321"/>
                but I am getting the output like this: <address name="" pin="54321" door="12345"/> and attributes order is keep on changing every time I build and run the application.

                1 Reply Last reply
                0
                • VRoninV VRonin

                  @Mallikarjun said in Incorrect order of xml attributes-Qt 5.6.2:

                  temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization

                  You misread the documentation. this does not mean it will be ordered. This ensures that if you run the application 2 times the elements will still be shuffled but in the same way i.e. it will be shuffled always in the same way but still shuffled.

                  I stand by:

                  programs should never depend on a particular QHash ordering

                  Why do you need ordered attributes?!

                  M Offline
                  M Offline
                  Mallikarjun
                  wrote on last edited by
                  #8

                  @VRonin
                  Why do you need ordered attributes?!
                  -----> It is a problem when you track changes of output XML files using GIT, SVN, etc

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    Below is my code :

                    if that is your real code I see no reason why you should use the xml module at all. QXmlStreamReader and QXmlStreamWriter from QtCore are way better suited for the task.

                    It is a problem when you track changes of output XML files

                    Use an XML tool like http://prettydiff.com/ and it will not show differences just because the order of the attributes changes

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1

                    • Login

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