Inconveniently, QVariant{"Test"}.canconvert<int>() returns true.
As far as I know, only QVariant::toInt(bool *ok) can tell, if the value actually can be converted to an int. But I cannot use this function in a template class when I don't know the type.
Is there a way to check if the value actually can be converted to a generic type?
I am not eniterly sure what do you mean with "dont know the type". You need to know the type for
cannconvert<int>just as you need to know the type forQVariant::toInt(bool *ok).I am not into this Qt facilities and I assume you simply need a way to use
QVariant::toInt(bool *ok)in generic code where you needQVariant::toTfor other typesT. Then you can do this:Where
my_canconvertis a function template with respective specializations:This is how you can provide your own generic customization of non-generic functions. However, as already mentioned, I am not into
QVariantand I didnt bother to look it up. I suspect that there is a better way to use it. The above is more like a general approach in case nothing else helps.