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 do I return "null" from C++ to QML?
Forum Updated to NodeBB v4.3 + New Features

How do I return "null" from C++ to QML?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.3k Views 2 Watching
  • 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.
  • P Offline
    P Offline
    pderocco
    wrote on last edited by
    #1

    If I write a C++ module that defines a QML object type, and it has a Q_INVOKABLE member function that returns a QVariant, what C++ return value gets converted to "null" in JavaScript?

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by Eddy
      #2

      nice and thorough explanation on this link

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pderocco
        wrote on last edited by
        #3

        @eddy Not exactly the same thing. That link talks about C++ producing a nullptr value when the expected type is pointer-to-something-derived-from-QObject, but I'm trying to return a QVariant, not a pointer to a QVariant.

        Ultimately, I want my Q_INVOKABLE member function to be able to return a QByteArray, a QString, or something that converts to null, so I can write in QML:

            var v = objId.memFunc()
            if (v === null)
                processNothing()
            else if (typeof(v) === "string")
                processMessage(v)
            else
                processData(new Uint8Array(v))
        

        It looks to me like QVariant is the correct type for memFunc() to return, but for all the conversions described by the docs, "null" seems to have been left out. Or am I wrong, and I'm supposed to be returning a pointer to a "new QVariant" pointer for the QByteArray and QString cases, and nullptr for the null case?

        1 Reply Last reply
        0

        • Login

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