Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to detect "url" type in QML/Javascript?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 467 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fcarney
    wrote on 23 Sept 2020, 18:46 last edited by
    #1

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

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 23 Sept 2020, 18:56 last edited by
      #2

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

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

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fcarney
        wrote on 23 Sept 2020, 19:07 last edited by fcarney
        #3

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

        C++ is a perfectly valid school of magic.

        G 1 Reply Last reply 24 Sept 2020, 10:12
        2
        • F fcarney
          23 Sept 2020, 19:07

          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;
          }
          
          G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 24 Sept 2020, 10:12 last edited by
          #4

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

          F 1 Reply Last reply 24 Sept 2020, 13:58
          2
          • G GrecKo
            24 Sept 2020, 10:12

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

            F Offline
            F Offline
            fcarney
            wrote on 24 Sept 2020, 13:58 last edited by
            #5

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

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0

            1/5

            23 Sept 2020, 18:46

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved