Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to use Qt documentation?
QtWS25 Last Chance

How to use Qt documentation?

Scheduled Pinned Locked Moved Solved C++ Gurus
documentationqtendian
16 Posts 6 Posters 2.3k 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.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #1

    Good evening,
    I can see that Qt has good documentation but not enough examples. Usually I understand how to use the fucntion but now I'm stuck...
    I'm trying to use the following function: Qt_ToBigEndian
    I can see the syntaxis:

    void qToBigEndian(const void *src, qsizetype count, void *dest)
    

    where (as I understood):
    *src - pointer to the my data that I need to convert to big endian
    count - number of bytes (or elements) to convert to big endian
    *dest - destination pointer (pointer to memory allocation where I want to get big-ordered data)

    So I write:

        int a[] = {1, 2, 3, 4, 5};
        int x[5];
        qToBigEndian(a, 5, x);
    

    and of course I get errors :)
    What is wrong?
    !______________________________________________________!
    main.cpp:12:5: error: no matching function for call to 'qToBigEndian'
    qendian.h:238:35: note: candidate template ignored: couldn't infer template argument 'T'
    qendian.h:225:49: note: candidate function template not viable: requires single argument 'source', but 3 arguments were provided
    qendian.h:233:35: note: candidate function template not viable: requires 2 arguments, but 3 were provided

    By the way the stuff:

        for (int i = 0; i < 5; i++)
        {
            cout << qToBigEndian(a[i]) << endl;
        }
    

    goes ok

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      That one is a bit tricky, i agree.
      It does not really scream "Hey its a template function."

      The docs says
      "
      Note: Template type T can either be a quint16, qint16, quint32, qint32, quint64, or qint64. Other types of integers, e.g., qlong, are not applicable."

      so something like

      quint32 a[] = {1, 2, 3, 4, 5};
      quint32 x[5];
      qToBigEndian<quint32>(a, 5,x);
      
      Please_Help_me_DP 1 Reply Last reply
      6
      • mrjjM mrjj

        Hi
        That one is a bit tricky, i agree.
        It does not really scream "Hey its a template function."

        The docs says
        "
        Note: Template type T can either be a quint16, qint16, quint32, qint32, quint64, or qint64. Other types of integers, e.g., qlong, are not applicable."

        so something like

        quint32 a[] = {1, 2, 3, 4, 5};
        quint32 x[5];
        qToBigEndian<quint32>(a, 5,x);
        
        Please_Help_me_DP Offline
        Please_Help_me_DP Offline
        Please_Help_me_D
        wrote on last edited by
        #3

        @mrjj thank you!
        In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

        JonBJ KroMignonK 2 Replies Last reply
        0
        • Please_Help_me_DP Please_Help_me_D

          @mrjj thank you!
          In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Please_Help_me_D
          Shoot me if I'm wrong, but I don't think float/doubles have any "endian". They are stored according to some IEEE standard.

          Please_Help_me_DP 1 Reply Last reply
          0
          • JonBJ JonB

            @Please_Help_me_D
            Shoot me if I'm wrong, but I don't think float/doubles have any "endian". They are stored according to some IEEE standard.

            Please_Help_me_DP Offline
            Please_Help_me_DP Offline
            Please_Help_me_D
            wrote on last edited by
            #5

            @JonB I don't have a gun :D
            but as far as I know (and according to my experience in Matlab) files can be stored in any type (int, float, double etc) and in either big- or little-endian
            Also my data maybe stored in IEEE format but there is also IBM format (it is old format but still actively used while storing SEGY files)

            JonBJ 1 Reply Last reply
            0
            • Please_Help_me_DP Please_Help_me_D

              @JonB I don't have a gun :D
              but as far as I know (and according to my experience in Matlab) files can be stored in any type (int, float, double etc) and in either big- or little-endian
              Also my data maybe stored in IEEE format but there is also IBM format (it is old format but still actively used while storing SEGY files)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Please_Help_me_D
              Apologies.
              See https://stackoverflow.com/questions/2945174/floating-point-endianness & https://stackoverflow.com/questions/2782725/converting-float-values-from-big-endian-to-little-endian. I think the latter shows you code to convert. Also https://stackoverflow.com/questions/1786137/c-serialization-of-the-floating-point-numbers-floats-doubles.

              Please_Help_me_DP 1 Reply Last reply
              3
              • JonBJ JonB

                @Please_Help_me_D
                Apologies.
                See https://stackoverflow.com/questions/2945174/floating-point-endianness & https://stackoverflow.com/questions/2782725/converting-float-values-from-big-endian-to-little-endian. I think the latter shows you code to convert. Also https://stackoverflow.com/questions/1786137/c-serialization-of-the-floating-point-numbers-floats-doubles.

                Please_Help_me_DP Offline
                Please_Help_me_DP Offline
                Please_Help_me_D
                wrote on last edited by
                #7

                @JonB Thank you!
                First of all I'm trying to use built-in function. And I liked Qt built in function because it can deal with array, that is what I like :)
                What <quint32> mean in this example?

                quint32 a[] = {1, 2, 3, 4, 5};
                quint32 x[5];
                qToBigEndian<quint32>(a, 5,x);
                
                JonBJ 1 Reply Last reply
                0
                • Please_Help_me_DP Please_Help_me_D

                  @mrjj thank you!
                  In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @Please_Help_me_D said in How to use Qt documentation?:

                  In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

                  As far as I know, a float is stored as a 32 bits word and a double as 64 bits word.
                  So my suggestion would be to cast your float value to quint32 and your double to quint64

                  float floatBE = static_cast<float>(qToBigEndian(static_cast<quint32>(val));
                  double doubleBE = static_cast<double>(qToBigEndian(static_cast<quint64>(valDouble));
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  Please_Help_me_DP aha_1980A 2 Replies Last reply
                  0
                  • Please_Help_me_DP Please_Help_me_D

                    @JonB Thank you!
                    First of all I'm trying to use built-in function. And I liked Qt built in function because it can deal with array, that is what I like :)
                    What <quint32> mean in this example?

                    quint32 a[] = {1, 2, 3, 4, 5};
                    quint32 x[5];
                    qToBigEndian<quint32>(a, 5,x);
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Please_Help_me_D
                    quint32 is (presumably) Qt's type for an unsigned 32-bit integer.
                    qToBigEndian<quint32> is written like that presumably because it's a template class/function.

                    1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @Please_Help_me_D said in How to use Qt documentation?:

                      In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

                      As far as I know, a float is stored as a 32 bits word and a double as 64 bits word.
                      So my suggestion would be to cast your float value to quint32 and your double to quint64

                      float floatBE = static_cast<float>(qToBigEndian(static_cast<quint32>(val));
                      double doubleBE = static_cast<double>(qToBigEndian(static_cast<quint64>(valDouble));
                      
                      Please_Help_me_DP Offline
                      Please_Help_me_DP Offline
                      Please_Help_me_D
                      wrote on last edited by
                      #10

                      @KroMignon thank you! that should work, few minuts I'm going to try it
                      @JonB please correct me if I misunderstood you, we need to write datatype in brackets <> to point that incoming (or outcoming??) parameter is in quint32 because qToBigEndian is a template function?
                      I just did:

                          double y[] = {1.442, 2.34, 1.56, 2.66, 68.88};
                          double yy[5];
                          qToLittleEndian<double>(y, 5, yy);
                      

                      and it doesn't output the error but I need to check the correctness of this

                      JonBJ 1 Reply Last reply
                      0
                      • Please_Help_me_DP Please_Help_me_D

                        @KroMignon thank you! that should work, few minuts I'm going to try it
                        @JonB please correct me if I misunderstood you, we need to write datatype in brackets <> to point that incoming (or outcoming??) parameter is in quint32 because qToBigEndian is a template function?
                        I just did:

                            double y[] = {1.442, 2.34, 1.56, 2.66, 68.88};
                            double yy[5];
                            qToLittleEndian<double>(y, 5, yy);
                        

                        and it doesn't output the error but I need to check the correctness of this

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Please_Help_me_D
                        It's not a "parameter" in the sense of passing to a function. You really need to go Google for C++ template if you wish to understand C++ templates.

                        Please_Help_me_DP 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @Please_Help_me_D
                          It's not a "parameter" in the sense of passing to a function. You really need to go Google for C++ template if you wish to understand C++ templates.

                          Please_Help_me_DP Offline
                          Please_Help_me_DP Offline
                          Please_Help_me_D
                          wrote on last edited by
                          #12

                          @JonB ok I look for that
                          I tryed to convert double precision point in Qt and in Matlab to check the correctness of little/big endian conversation. So in Qt:

                              double y[] = {1.442, 2.34, 1.56, 2.66, 68.88};
                              double yy[5];
                              qToLittleEndian<double>(y, 5, yy);
                          

                          yy = {-1.291069932956366e+151, -2.242484837845019e-38, -1.498274909768364e+261, 1.217960639251452e+43, -2.242484835660769e-38};

                          In Matlab:

                          // Matlab code
                          y = [1.442, 2.34, 1.56, 2.66, 68.88];
                          yy = swapbytes(y); // should rearrange the byte order [swapbyte_documentation](https://www.mathworks.com/help/matlab/ref/swapbytes.html)
                          

                          yy = [-1.29106993295637e+151, -2.24248483784502e-38, -1.49827490976836e+261, 1.21796063925145e+43, -2.24248483566077e-38];

                          The Matlab result is very close to Qt result. So I think that Qt function qToLittleEndian works not only with qType but with other standart types as well

                          1 Reply Last reply
                          0
                          • fcarneyF Offline
                            fcarneyF Offline
                            fcarney
                            wrote on last edited by
                            #13

                            @Please_Help_me_D said in How to use Qt documentation?:

                            I need to check the correctness of this

                            Look up how a given value is stored in big endian and little endian. Then print out the bytes of your variable as hex. Do this before and after conversion. You should be able to see that it rearranges the bytes in an expected way. It might be worthwhile to create your own function to do this as well. So you understand more what is going on. It might also be worthwhile to templatize your function so you can see how templates work too.

                            C++ is a perfectly valid school of magic.

                            Please_Help_me_DP 1 Reply Last reply
                            1
                            • fcarneyF fcarney

                              @Please_Help_me_D said in How to use Qt documentation?:

                              I need to check the correctness of this

                              Look up how a given value is stored in big endian and little endian. Then print out the bytes of your variable as hex. Do this before and after conversion. You should be able to see that it rearranges the bytes in an expected way. It might be worthwhile to create your own function to do this as well. So you understand more what is going on. It might also be worthwhile to templatize your function so you can see how templates work too.

                              Please_Help_me_DP Offline
                              Please_Help_me_DP Offline
                              Please_Help_me_D
                              wrote on last edited by
                              #14

                              @fcarney thank you. I already did it with the help of Matlab above. I just converted the same numbers of double precision from Big to Little and the result was the same.
                              I understand how it works, it is clear from the definition of Little/Big endian and definition of byte

                              1 Reply Last reply
                              1
                              • KroMignonK KroMignon

                                @Please_Help_me_D said in How to use Qt documentation?:

                                In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

                                As far as I know, a float is stored as a 32 bits word and a double as 64 bits word.
                                So my suggestion would be to cast your float value to quint32 and your double to quint64

                                float floatBE = static_cast<float>(qToBigEndian(static_cast<quint32>(val));
                                double doubleBE = static_cast<double>(qToBigEndian(static_cast<quint64>(valDouble));
                                
                                aha_1980A Offline
                                aha_1980A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @KroMignon said in How to use Qt documentation?:

                                @Please_Help_me_D said in How to use Qt documentation?:

                                In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

                                As far as I know, a float is stored as a 32 bits word and a double as 64 bits word.
                                So my suggestion would be to cast your float value to quint32 and your double to quint64

                                float floatBE = static_cast<float>(qToBigEndian(static_cast<quint32>(val));
                                double doubleBE = static_cast<double>(qToBigEndian(static_cast<quint64>(valDouble));
                                

                                nope, that does NOT work! Casting float to int truncates it. What you can do instead, is to reinterpret_cast the memory occupied by the float - or use a union. We currently have a similar topic in the forum on this.

                                Regards

                                Qt has to stay free or it will die.

                                1 Reply Last reply
                                3
                                • Please_Help_me_DP Offline
                                  Please_Help_me_DP Offline
                                  Please_Help_me_D
                                  wrote on last edited by Please_Help_me_D
                                  #16

                                  I have written the following code:

                                      if (fileEndian == "Little"){
                                          for(quint32 i = 0; i < nTrc; i++){
                                              FFID(i) = *util::bit_cast<qint32*>(qFromLittleEndian(qFile->map(3608+i*bytesPerTrc, 1)));
                                          }
                                      } else if (fileEndian == "Big"){
                                          for(quint32 i = 0; i < nTrc; i++){
                                              FFID(i) = *util::bit_cast<qint32*>(qFromBigEndian(qFile->map(3608+i*bytesPerTrc, 1)));
                                          }
                                      }
                                  

                                  It gives me errors:

                                  • readsegy.obj:-1: ошибка: LNK2019: unresolved external symbol "unsigned char * __cdecl qbswap<unsigned char *>(unsigned char *)" (??$qbswap@PEAE@@YAPEAEPEAE@Z) referenced in function "unsigned char * __cdecl qFromBigEndian<unsigned char *>(unsigned char *)" (??$qFromBigEndian@PEAE@@YAPEAEPEAE@Z)
                                  • debug\ReadSegy.exe:-1: ошибка: LNK1120: 1 unresolved externals

                                  The compilator output:
                                  jom: C: \ Users \ tasik \ Documents \ Qt_Projects \ build-ReadSegy-Desktop_x86_windows_msvc2017_pe_64bit-Debug \ Makefile.Debug [debug \ ReadSegy.exe] Error 1120
                                  jom: C: \ Users \ tasik \ Documents \ Qt_Projects \ build-ReadSegy-Desktop_x86_windows_msvc2017_pe_64bit-Debug \ Makefile [debug] Error 2
                                  19:05:31: The process "C: \ Qt \ Tools \ QtCreator \ bin \ jom.exe" ended with code 2.
                                  Error during assembly / deployment of ReadSegy project (bundle: Desktop (x86-windows-msvc2017-pe-64bit))
                                  During the execution of the "Assembly"

                                  Actually the problem was that I didn't know the line which throws these errors but by intuation I just commented the BigEndian part of the code and it works:

                                      if (fileEndian == "Little"){
                                          for(quint32 i = 0; i < nTrc; i++){
                                              FFID(i) = *util::bit_cast<qint32*>(qFromLittleEndian(qFile->map(3608+i*bytesPerTrc, 1)));
                                          }
                                      } else if (fileEndian == "Big"){/*
                                          for(quint32 i = 0; i < nTrc; i++){
                                              FFID(i) = *util::bit_cast<qint32*>(qFromBigEndian(qFile->map(3608+i*bytesPerTrc, 1)));
                                          }*/
                                      }
                                  

                                  I use little endian Windows 10 x64, Qt 5.14.0, MSVC 2017 x64.
                                  Why do I can use qFromLittleEndian but I can't qFromBigEndian??

                                  By the way the endian of my file is LITTLE now

                                  I think I just found a solution. If I change the order of performing bit_cast and qFromBigEndian it works:

                                      if (fileEndian == "Little"){
                                          for(quint32 i = 0; i < nTrc; i++){
                                              FFID(i) = qFromLittleEndian(*util::bit_cast<qint32*>(qFile->map(3608+i*bytesPerTrc, 1)));
                                          }
                                      } else if (fileEndian == "Big"){
                                          for(quint32 i = 0; i < nTrc; i++){
                                              FFID(i) = qFromBigEndian(*util::bit_cast<qint32*>(qFile->map(3608+i*bytesPerTrc, 1)));
                                          }
                                      }
                                  

                                  I don't understand why but that works fine

                                  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