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. Direct comparing of two QVariant variables
Qt 6.11 is out! See what's new in the release blog

Direct comparing of two QVariant variables

Scheduled Pinned Locked Moved General and Desktop
66 Posts 9 Posters 49.2k Views 2 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #23

    [quote author="deisik" date="1326660493"]Why should the compiler know anything if the return type can just be a void* pointer of that very QVariant::value() without explicit type cast (or call it valueVoid() for that matter)? I don't mean overloading and again, I see no problem here - qt knows the right type, so does the programmer[/quote]
    But that would not help you solve your problem, I think? I mean, comparing two void pointers would compare their addresses, not their contents. So, that would force to to start casting again, and then you're back to where you started.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #24

      And what do you want to do with the void pointer? That is as opaque as the QVariant itself...

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • deisikD Offline
        deisikD Offline
        deisik
        wrote on last edited by
        #25

        [quote author="Andre" date="1326661453"]But that would not help you solve your problem, I think? I mean, comparing two void pointers would compare their addresses, not their contents. So, that would force to to start casting again, and then you're back to where you started. [/quote]

        Yes, there is a problem here but it deserves and requires thorough thinking over. At least we are no longer bound to QVariant without losing its advantages

        1 Reply Last reply
        0
        • deisikD Offline
          deisikD Offline
          deisik
          wrote on last edited by
          #26

          [quote author="Volker" date="1326661563"]And what do you want to do with the void pointer? That is as opaque as the QVariant itself...[/quote]

          I can ask you another question, how QVariant itself stores its value data type?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #27

            [quote author="deisik" date="1326661825"]
            Yes, there is a problem here but it deserves and requires thorough thinking over. At least we are no longer bound to QVariant without losing its advantages[/quote]

            When passing a void pointer to another method (operators are methods too) you even loos the information that it actually is a QVariant. We're all looking forward to see your implementation outline of a lessThan implementation taking to void pointers, though. I'm pretty sure neither Andre, nor me or anyone else we both know a decent solution, be we're all eager to learn something new (as everyone doing software development should do).

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #28

              A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #29

                [quote author="deisik" date="1326662003"][quote author="Volker" date="1326661563"]And what do you want to do with the void pointer? That is as opaque as the QVariant itself...[/quote]

                I can ask you another question, how QVariant itself stores its value data type?

                [/quote]

                http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • deisikD Offline
                  deisikD Offline
                  deisik
                  wrote on last edited by
                  #30

                  [quote author="Andre" date="1326662334"]A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution. [/quote]

                  This information about data type is known beside QVariant (read saved somewhere else) - this is the whole point of my question. How to use it for comparison and whether it is possible in c++ at all is another question

                  1 Reply Last reply
                  0
                  • deisikD Offline
                    deisikD Offline
                    deisik
                    wrote on last edited by
                    #31

                    [quote author="Volker" date="1326662484"]http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358[/quote]

                    I expected to see something like that

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #32

                      [quote author="deisik" date="1326662787"][quote author="Andre" date="1326662334"]A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution. [/quote]

                      This information about data type is known beside QVariant (read saved somewhere else) - this is the whole point of my question. How to use it for comparison and whether it is possible in c++ at all is another question[/quote]

                      You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours. Our opinion is known (void pointers make matters worse). The other constraints, set by C++, have already been pointed out.

                      [quote author="deisik" date="1326662903"][quote author="Volker" date="1326662484"]http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358[/quote]

                      I expected to see something like that

                      [/quote]

                      Reading source code is the best way of understanding things. What else would you have hoped to see instead of your expectations?

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • deisikD Offline
                        deisikD Offline
                        deisik
                        wrote on last edited by
                        #33

                        [quote author="Volker" date="1326663446"]
                        You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours. Our opinion is known (void pointers make matters worse). The other constraints, set by C++, have already been pointed out.[/quote]

                        Ok, I will make a try

                        [quote author="Volker" date="1326663446"]Reading source code is the best way of understanding things. What else would you have hoped to see instead of your expectations?[/quote]

                        To hope for something out of nothing is the most popular form of hope. Black magic indeed

                        1 Reply Last reply
                        0
                        • deisikD Offline
                          deisikD Offline
                          deisik
                          wrote on last edited by
                          #34

                          [quote author="Volker" date="1326663446"]
                          You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours[/quote]

                          My try as promised. I am interested in comparing ints, floats and strings and as I said earlier data type for each value is known. This means that we know beforehand the length the byte array representing this or that value takes in memory. So the algorithm for comparing two values is to regard them as, say, 64 bits integers, filling the higher missing bits of the shorter values (copy of) with zeroes (to clean up garbage)

                          This technique should work even for real numbers. In fact, interpreting the bit-pattern of a floating-point number as integer is one of the methods for comparing floats and doubles (as a side note, you cannot correctly compare two floats by comparing them "out of the box" as you suggested in the code snippet, but I think you know this yourself)

                          There still remains a minor problem with negative numbers but as it happens I am not much interested in them (in my case there are none), furthermore I guess this can be fixed somehow

                          Now you give me a void pointer to a QVariant value and I show you the code

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

                            [quote]I am interested in comparing ints, floats and strings and as I said earlier data type for each value is known.[/quote]

                            Do you mean that the variants you want to compare are only of these three types? An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)

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

                            1 Reply Last reply
                            0
                            • deisikD Offline
                              deisikD Offline
                              deisik
                              wrote on last edited by
                              #36

                              [quote author="peppe" date="1326672292"]Do you mean that the variants you want to compare are only of these three types? An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)[/quote]

                              There can be no comparison between defferent types (imagine a table where you compare only between values of the same column). And yes, columns of this imaginary table are almost all kinds of unsigned base types but they are all interpreted as quint64 integer for saving in a binary file (well, this is a waste of space but it saves time when retrieving them and when we come to file sizes over a few GBs it makes no difference - you just save the value in place of otherwise a 64 bit pointer)

                              [quote author="peppe" date="1326672292"]An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)[/quote]

                              Yes, arbitrary text strings. In case there is a "number" inside it is valid to consider it as text. So "75" would actually be 55 53 as a byte array

                              There are also QDateTimes but they are converted to uints with QDateTime::toTime_t(). In fact, all these types pass in QVariant as raw bytes (by QByteArray) and converted to real int, floats, strings and dates only for representation through QAbstractItemModel and building indexes (where comparison comes into play). It is a matter of interpretation

                              1 Reply Last reply
                              0
                              • deisikD Offline
                                deisikD Offline
                                deisik
                                wrote on last edited by
                                #37

                                And it seems now that I can really make comparisons without QVariant entirely through byte arrays

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #38

                                  I am still wondering how this byte comparison is going to help you achieve the goal you stated earlier:

                                  [quote author="deisik" date="1326624029"]How can this help me find out which value is greater and which is less?[/quote]

                                  How, you think, is a byte-level comparision going to help you compare two QStrings stored in that QVariant union? I mean: you do realize that the actual character array is not stored within the QVariant, right?

                                  1 Reply Last reply
                                  0
                                  • deisikD Offline
                                    deisikD Offline
                                    deisik
                                    wrote on last edited by
                                    #39

                                    [quote author="Andre" date="1326703007"]How, you think, is a byte-level comparision going to help you compare two QStrings stored in that QVariant union? I mean: you do realize that the actual character array is not stored within the QVariant, right?
                                    [/quote]

                                    Right, it is stored somewhere in memory, so rises the question of a void* pointer to this character array. You seem to be missing the whole point of what I say

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #40

                                      Then, I guess we just don't understand each other. I wish you all the luck with your attempts to efficiently compare QVariants, but I don't think I can contribute to this discussion anymore. Please let us know if you find an efficient solution, I would be interested to see it.

                                      1 Reply Last reply
                                      0
                                      • deisikD Offline
                                        deisikD Offline
                                        deisik
                                        wrote on last edited by
                                        #41

                                        [quote author="Andre" date="1326707128"]Then, I guess we just don't understand each other. I wish you all the luck with your attempts to efficiently compare QVariants, but I don't think I can contribute to this discussion anymore. Please let us know if you find an efficient solution, I would be interested to see it. [/quote]

                                        I have already shown a clean solution which I am going to pick up myself. You probably confuse a pointer to a QVariant object as such with a pointer to data which this object wraps around and to which it should provide this pointer (as a void*) - I talk about the latter

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          giesbert
                                          wrote on last edited by
                                          #42

                                          [quote author="deisik" date="1326707907"]I have already shown a clean solution which I am going to pick up myself. You probably confuse a pointer to a QVariant object as such with a pointer to data which this object wraps around and should provide as a void* - I talk about the latter[/quote]

                                          If I understood correctly, you want to use the pointer to the memory to do the comparison, right?
                                          How do you know, how to interpret the raw memory segment? The memory representation of a double is different from an int is different from .... So you again need the information to do correct comparison. Where is the benefit?

                                          Nokia Certified Qt Specialist.
                                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                          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