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 to call QVariant::convert
Forum Updated to NodeBB v4.3 + New Features

how to call QVariant::convert

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.4k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    hi,
    I'm struggling to calling QVariant::convert

    passing 'const QVariant' as 'this' argument discards qualifiers [-fpermissive]
    

    callback method where i recive a QVariant and want to convert it to QVariantList

    void Client::listUpdated(const QVariant &value){
      
        if(value.canConvert(QVariant::List) && value.convert(QVariant::List) ){
                setList(value.toList());
        }else{
            //error ..
        }
    }
    

    Im i calling convert with wrong argument ? what should i pass to it?

    JonBJ 1 Reply Last reply
    0
    • ODБOïO ODБOï

      @J-Hilk but if i understand this part of doc correctly, they say i have to use canConvert And convert, because even if "canConvert" returns "true", "convert" can still return "false". Did i get it wrong?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #8

      @LeLev in principle, yes,
      but if toList fails, you get an empty list. If you want to, you can check for that as well

      void listUpdated(const QVariant &value){
          if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){
            setList(list);
          }
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      ODБOïO JonBJ 2 Replies Last reply
      1
      • ODБOïO ODБOï

        hi,
        I'm struggling to calling QVariant::convert

        passing 'const QVariant' as 'this' argument discards qualifiers [-fpermissive]
        

        callback method where i recive a QVariant and want to convert it to QVariantList

        void Client::listUpdated(const QVariant &value){
          
            if(value.canConvert(QVariant::List) && value.convert(QVariant::List) ){
                    setList(value.toList());
            }else{
                //error ..
            }
        }
        

        Im i calling convert with wrong argument ? what should i pass to it?

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

        @LeLev
        You can't QVariant::convert() a const QVariant & --- it changes it!! Make the parameter non-const or work on a copy or similar.

        ODБOïO 1 Reply Last reply
        2
        • JonBJ JonB

          @LeLev
          You can't QVariant::convert() a const QVariant & --- it changes it!! Make the parameter non-const or work on a copy or similar.

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #3

          @JonB hi,
          thanks for your answer,
          should i create a local variable like this ?

          void Client::listUpdated(const QVariant &value){
              QVariant tmpValue = value;
              if(value.canConvert(QVariant::List) && tmpValue .convert(QVariant::List) ){
                setList(value.toList());
              }
          }
          
          J.HilkJ 1 Reply Last reply
          0
          • ODБOïO ODБOï

            @JonB hi,
            thanks for your answer,
            should i create a local variable like this ?

            void Client::listUpdated(const QVariant &value){
                QVariant tmpValue = value;
                if(value.canConvert(QVariant::List) && tmpValue .convert(QVariant::List) ){
                  setList(value.toList());
                }
            }
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #4

            @LeLev in your case, I would drop it all together

            canConvert (QVariant::List) + toList() is in essence the same as convert(QVariant::List) with the difference that convert modifies itself and toList returns a modified Value


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            ODБOïO 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @LeLev in your case, I would drop it all together

              canConvert (QVariant::List) + toList() is in essence the same as convert(QVariant::List) with the difference that convert modifies itself and toList returns a modified Value

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #5

              @J-Hilk hi
              Thx for the suggestion.
              If i understand correctly, this will do ?

              void Client::listUpdated(const QVariant &value){
                  QVariant tmpValue = value;
                  if(tmpValue.canConvert(QVariant::List) && tmpValue.convert(QVariant::List) ){
                    setList(tmpValue );
                  }
              }
              
              J.HilkJ 1 Reply Last reply
              0
              • ODБOïO ODБOï

                @J-Hilk hi
                Thx for the suggestion.
                If i understand correctly, this will do ?

                void Client::listUpdated(const QVariant &value){
                    QVariant tmpValue = value;
                    if(tmpValue.canConvert(QVariant::List) && tmpValue.convert(QVariant::List) ){
                      setList(tmpValue );
                    }
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #6

                @LeLev it should, but I suggested:

                void Client::listUpdated(const QVariant &value){
                    if(value.canConvert(QVariant::List)  ){
                      setList(value.toList());
                    }
                }
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                ODБOïO 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @LeLev it should, but I suggested:

                  void Client::listUpdated(const QVariant &value){
                      if(value.canConvert(QVariant::List)  ){
                        setList(value.toList());
                      }
                  }
                  
                  ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by
                  #7

                  @J-Hilk but if i understand this part of doc correctly, they say i have to use canConvert And convert, because even if "canConvert" returns "true", "convert" can still return "false". Did i get it wrong?

                  J.HilkJ 1 Reply Last reply
                  0
                  • ODБOïO ODБOï

                    @J-Hilk but if i understand this part of doc correctly, they say i have to use canConvert And convert, because even if "canConvert" returns "true", "convert" can still return "false". Did i get it wrong?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #8

                    @LeLev in principle, yes,
                    but if toList fails, you get an empty list. If you want to, you can check for that as well

                    void listUpdated(const QVariant &value){
                        if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){
                          setList(list);
                        }
                    }
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    ODБOïO JonBJ 2 Replies Last reply
                    1
                    • J.HilkJ J.Hilk

                      @LeLev in principle, yes,
                      but if toList fails, you get an empty list. If you want to, you can check for that as well

                      void listUpdated(const QVariant &value){
                          if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){
                            setList(list);
                          }
                      }
                      
                      ODБOïO Offline
                      ODБOïO Offline
                      ODБOï
                      wrote on last edited by
                      #9

                      @J-Hilk Thank you for clarifying that

                      1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @LeLev in principle, yes,
                        but if toList fails, you get an empty list. If you want to, you can check for that as well

                        void listUpdated(const QVariant &value){
                            if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){
                              setList(list);
                            }
                        }
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @J-Hilk said in how to call QVariant::convert:

                        if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){

                        I know it works, but ... really? :) First call toList() on value, then afterward check whether it could convert. Are you in some backward time-warp? ;-)

                        J.HilkJ 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @J-Hilk said in how to call QVariant::convert:

                          if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){

                          I know it works, but ... really? :) First call toList() on value, then afterward check whether it could convert. Are you in some backward time-warp? ;-)

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #11

                          @JonB fair enough, is this particular case, 2nd if case inside the first one would probably be more sensible :D


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          JonBJ 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @JonB fair enough, is this particular case, 2nd if case inside the first one would probably be more sensible :D

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

                            @J-Hilk
                            It made me smile. I have never declared a variable inside an if ( condition. Witches' brew :) I can sort of see you'd have to do it your way round to achieve it. That's what you get for putting a ; inside an if ( :D Personally I'm not going to call toList() till after convert() has cleared me to do it.

                            J.HilkJ 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @J-Hilk
                              It made me smile. I have never declared a variable inside an if ( condition. Witches' brew :) I can sort of see you'd have to do it your way round to achieve it. That's what you get for putting a ; inside an if ( :D Personally I'm not going to call toList() till after convert() has cleared me to do it.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #13

                              @JonB you mean:

                              void Client::listUpdated(QVariant value){
                                  if(value.canConvert(QVariant::List) && value.convert(QVariant::List) ){
                                    setList(value);
                                  }
                              }
                              

                              I would agree, sometimes it's just simpler to pass by value and rely on the copy on write functionality of Qt-classes 😉


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              JonBJ 1 Reply Last reply
                              1
                              • J.HilkJ J.Hilk

                                @JonB you mean:

                                void Client::listUpdated(QVariant value){
                                    if(value.canConvert(QVariant::List) && value.convert(QVariant::List) ){
                                      setList(value);
                                    }
                                }
                                

                                I would agree, sometimes it's just simpler to pass by value and rely on the copy on write functionality of Qt-classes 😉

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

                                @J-Hilk
                                Well, no, you/someone said earlier we don't need to modify the QVariant value input parameter. You/someone said value.toList() was OK on a const. But we ought verify it's convertible before doing so/to distinguish from convertible-but-empty list. So I just thought (untested):

                                if (value.canConvert(QVariant::List))
                                    setList(value.toList());
                                

                                ?

                                Oh, that's what you started with? Yeah I see, I read that link now. Well, toList() is not going to fail here after canConvert() :)

                                Anyway, that wasn't really my amusement. It was calling toList() and then calling canConvert(), as far as I can see in order to fit it into a variable-defining if(. There's no big point here from me, I get now that the canConvert/convert() functions aren't the same as each other.

                                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