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. [solved] How to check for null contextProperty in QML?

[solved] How to check for null contextProperty in QML?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 14.0k 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
    fxam
    wrote on 1 Aug 2014, 01:39 last edited by
    #1

    Sometimes I need to setContextProperty("juice", nullptr). And I don't want JavaScript in QML to output any error. So I have tried the following in QML to check for null, but there is still an error output:
    @
    ToolButton {
    text: {
    if ( !juice ) { // trying to check for null, but there is still an error: ReferenceError: juice is not defined
    return "N/A";
    }
    return juice.name;
    }
    }
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 1 Aug 2014, 07:38 last edited by
      #2

      @
      if (juice == undefined)
      @

      (Z(:^

      1 Reply Last reply
      1
      • P Offline
        P Offline
        p3c0
        Moderators
        wrote on 1 Aug 2014, 09:50 last edited by
        #3

        To add to sierdzio
        @
        if(typeof(juice)=="undefined")
        @

        157

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fxam
          wrote on 1 Aug 2014, 13:18 last edited by
          #4

          Thanks guys! This is working, no error at all:
          @
          if ( typeof(juice) == "undefined" || !juice )
          @

          This is not working though, still got ReferenceError: juice is not defined:
          @
          if (juice == undefined || !juice)
          @

          Here's what I found:

          Before any setContextProperty("juice", ...) is called, typeof(juice) will return “undefined”.

          After setContextProperty("juice", nullptr) is called, typeof(juice) returns “object”, but juice equals to null.

          After setContextProperty("juice", apple) is called, typeof(juice) returns "object", and juice equals to apple, of course.

          1 Reply Last reply
          1
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 1 Aug 2014, 15:06 last edited by
            #5

            Interesting find. Thanks for sharing :)

            157

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fxam
              wrote on 1 Aug 2014, 22:21 last edited by
              #6

              Ah well, after some pondering on the findings, I resorted to the following way:

              Always set context before loading QML, even if it's just a setContextProperty("juice", nullptr). This is just like we always initialize class fields before usage.

              Then the simplest check can be used in QML:

              @
              if ( !juice ) {
              @

              1 Reply Last reply
              1

              2/6

              1 Aug 2014, 07:38

              4 unread
              • Login

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