How to detect "url" type in QML/Javascript?
-
wrote on 23 Sept 2020, 18:46 last edited by
I am passing a QUrl from my C++ code to my javascript code. However, I cannot detect the type
This does not work:
if(obj instanceof url)
This just detects that it is an object:
if(typeof obj === "object")
I need to know if the object is a url type. I am parsing objects in a recursive loop to build a properties list to feed to a model. So I am parsing into objects that contain urls. There are no properties on the url object. It just has a method for converting toString().
-
wrote on 23 Sept 2020, 18:56 last edited by
And this doesn't work either as javascript objects have a toString method:
if(typeof obj.toString === 'function')
-
wrote on 23 Sept 2020, 19:07 last edited by fcarney
Okay, I have a contextObject with misc functions. I added this for detecting this:
bool UtilFunctions::isUrl(QVariant obj)
{
if(obj.toUrl().isEmpty() || !obj.toUrl().isValid())
return false;
return true;
}Edit:
bool UtilFunctions::isUrl(QVariant obj) { if(obj.type() != QVariant::Url) return false; return true; }
-
Okay, I have a contextObject with misc functions. I added this for detecting this:
bool UtilFunctions::isUrl(QVariant obj)
{
if(obj.toUrl().isEmpty() || !obj.toUrl().isValid())
return false;
return true;
}Edit:
bool UtilFunctions::isUrl(QVariant obj) { if(obj.type() != QVariant::Url) return false; return true; }
so
return obj.type() == QVariant::Url;
;)
1/5