how to call QVariant::convert
-
hi,
I'm struggling to calling QVariant::convertpassing '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?
-
@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?
@LeLev in principle, yes,
but if toList fails, you get an empty list. If you want to, you can check for that as wellvoid listUpdated(const QVariant &value){ if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){ setList(list); } } -
hi,
I'm struggling to calling QVariant::convertpassing '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?
-
@LeLev
You can'tQVariant::convert()aconstQVariant &--- it changes it!! Make the parameter non-constor 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()); } } -
@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()); } }@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
-
@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 ); } }@LeLev it should, but I suggested:
void Client::listUpdated(const QVariant &value){ if(value.canConvert(QVariant::List) ){ setList(value.toList()); } } -
@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?
-
@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?
@LeLev in principle, yes,
but if toList fails, you get an empty list. If you want to, you can check for that as wellvoid listUpdated(const QVariant &value){ if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){ setList(list); } } -
@LeLev in principle, yes,
but if toList fails, you get an empty list. If you want to, you can check for that as wellvoid listUpdated(const QVariant &value){ if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){ setList(list); } } -
@LeLev in principle, yes,
but if toList fails, you get an empty list. If you want to, you can check for that as wellvoid listUpdated(const QVariant &value){ if(auto list = value.toList(); value.canConvert(QVariant::List) && !list.isEmpty()){ setList(list); } }@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()onvalue, then afterward check whether it could convert. Are you in some backward time-warp? ;-) -
@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()onvalue, then afterward check whether it could convert. Are you in some backward time-warp? ;-) -
@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 anif (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 anif (:D Personally I'm not going to calltoList()till afterconvert()has cleared me to do it. -
@J-Hilk
It made me smile. I have never declared a variable inside anif (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 anif (:D Personally I'm not going to calltoList()till afterconvert()has cleared me to do it.@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 😉
-
@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 theQVariant valueinput parameter. You/someone saidvalue.toList()was OK on aconst. 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 aftercanConvert():)Anyway, that wasn't really my amusement. It was calling
toList()and then callingcanConvert(), as far as I can see in order to fit it into a variable-definingif(. There's no big point here from me, I get now that thecanConvert/convert()functions aren't the same as each other.