Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to call QVariant::convert



  • 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?


  • Moderators

    @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);
        }
    }
    


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



  • @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());
        }
    }
    

  • Moderators

    @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



  • @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 );
        }
    }
    

  • Moderators

    @LeLev it should, but I suggested:

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


  • @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?


  • Moderators

    @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);
        }
    }
    


  • @J-Hilk Thank you for clarifying that



  • @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? ;-)


  • Moderators

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



  • @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.


  • Moderators

    @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 😉



  • @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.


Log in to reply