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

How to detect "url" type in QML/Javascript?



  • Docs on url

    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().



  • And this doesn't work either as javascript objects have a toString method:

    if(typeof obj.toString === 'function')
    


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

  • Qt Champions 2018

    so return obj.type() == QVariant::Url; ;)



  • @GrecKo Yeah, I changed my copy to that right after. Thanks for updating it!


Log in to reply