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. Correct way to do test for null in QML?
Forum Updated to NodeBB v4.3 + New Features

Correct way to do test for null in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 1.9k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    In QML I have a property:

        id: root
        property var anchorHorzCenter: parent.anchors.horizontalCenter
        property var anchorTop: parent.anchors.top
    

    Usage in the same QML:

        Text {
            id: rText
            anchors.horizontalCenter: {
                if ( root.anchorHorzCenter !== 0 ) {
                    return root.anchorHorzCenter
                }
            }
            anchors.top: {
                if ( root.anchorTop !== 0 ) {
                    return root.anchorTop
                }
            }
    

    This can't correct because when I run the application I get:

    qrc:/StaleDataModel.qml:41:5: QML QQuickText: Cannot anchor to a null item. qrc:/StaleDataModel.qml: 41
    

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      This is the QML I have:

      import QtQuick 2.0
      import QtQuick.Controls 2.15
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Layouts 1.3
      import QtQuick.Extras 1.4
      import QtQuick.Window 2.12
      import SimonsCPP 1.0
      
      Item {
          id: root
          property var fontPointSize: 30
          property string label
          property var percision: 4
          property real textWidth: 80
          property var value: simonP.value
      
          function checkStaleStatus(strToolTip) {
              if ( typeof strToolTip != "string" ) {
                  simonP.stale()
                  return
              }
              rImage.ToolTip.text = strToolTip
              rImage.visible = (strToolTip.length > 0)
      
              if ( rImage.visible === true ) {
                  rText.color = simonP.strStale()
              } else {
                  rText.color = simonP.strNormal()
              }
          }
          function rad2Deg(rad_) {
            return rad_ * 180.0 /  Math.PI;
          }
          function setPrecision (val_, sigDigit_) {
              var precision = Math.abs (val_) >= 1000 ? sigDigit_ - 4 :
              Math.abs (val_) >= 100 ? sigDigit_ - 3 :
              Math.abs (val_) >= 10 ? sigDigit_ - 2 :
              2;
      
            if (precision < 0)
              precision = 0;
      
            return val_.toFixed (precision);
          }
          function setValue(newValue) {
              simonP.setValue(newValue)
              rText.text = setPrecision (newValue, percision)
          }
      
          StaleData {
              id: simonP
              onValueChanged: {
                  checkStaleStatus(strToolTip)
              }
          }
      
          Text {
              id: rText
              anchors {
                  top: parent.top
                  right: parent.right
              }
              verticalAlignment: Text.AlignTop
              horizontalAlignment: Text.AlignLeft
              font.pointSize: root.fontPointSize
              text: { root.value
                      ? String(setPrecision (rad2Deg (root.value), percision))
                      : "N/A"
              }       
              width: root.textWidth
          }
      
          Label {
              id: rLabel
              text: root.label
              anchors.top: rText.bottom
              verticalAlignment: Text.AlignTop
              horizontalAlignment: Text.AlignLeft
              font.pointSize: root.fontPointSize
              width: parent.width
              color: "#FF000000"
          }
      
          Timer {
              interval: 250
              running: true
              repeat: true
              onTriggered: simonP.stale()
          }
      
          Image {
              id: rImage
              visible:  false
              anchors.left: rText.right
              source: "/stale.svg"
              ToolTip.visible: ToolTip.text.length > 0 && ma.containsMouse
      
              MouseArea {
                  id: ma
                  anchors.fill: parent
                  hoverEnabled: true
                  onContainsMouseChanged: checkStaleStatus()
              }
          }
      }
      

      Usage:

      import QtQuick 2.0
      import QtQuick.Controls 2.15
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Layouts 1.3
      import QtQuick.Extras 1.4
      import QtQuick.Window 2.12
      import SimonsCPP 1.0
      
      Window {
          id: root
          visible: true
          width: 640
          height: 480
          title: qsTr("Playground / Staging Arena")
      
          StaleDataModel {
              id:hdg
              label: "HDG"
              anchors {
                  left: parent.left
                  top: parent.top
              }
              fontPointSize: parent.width < 150 ? 30 : 16
              textWidth: 256
          }
      
          Button {
              id: btnData
              text: "Click Me..."
              anchors.top: hdg.anchors.bottom
              onClicked: {
                  hdg.setValue(parseFloat(hdg.value) + 0.01);
              }
          }
      }
      

      What I want to achieve is the data over the label with the icon to the right of the data label. What I actually get is the icon under the label and no data showing at all.

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #6

      @SPlatten , SOLVED:

      StaleDataModel.qml:

      import QtQuick 2.0
      import QtQuick.Controls 2.15
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Layouts 1.3
      import QtQuick.Extras 1.4
      import QtQuick.Window 2.12
      import SimonsCPP 1.0
      
      Item {
          id: root
          property var fontPointSize: 16
          property string label
          property string labelColor: "#FF000000"
          property var percision: 4
          property real textWidth: 80
          property var value: simonP.value
      
          function checkStaleStatus(strToolTip) {
              if ( typeof strToolTip != "string" ) {
                  simonP.stale()
                  return
              }
              rImage.ToolTip.text = strToolTip
              rImage.visible = (strToolTip.length > 0)
      
              if ( rImage.visible === true ) {
                  rText.color = simonP.strStale()
              } else {
                  rText.color = simonP.strNormal()
              }
          }
          function rad2Deg(rad_) {
            return rad_ * 180.0 /  Math.PI;
          }
          function setPrecision (val_, sigDigit_) {
              var precision = Math.abs (val_) >= 1000 ? sigDigit_ - 4 :
              Math.abs (val_) >= 100 ? sigDigit_ - 3 :
              Math.abs (val_) >= 10 ? sigDigit_ - 2 :
              2;
      
            if (precision < 0)
              precision = 0;
      
            return val_.toFixed (precision);
          }
          function setValue(newValue) {
              simonP.setValue(newValue)
              rText.text = setPrecision (newValue, percision)
          }
      
          StaleData {
              id: simonP
              onValueChanged: {
                  checkStaleStatus(strToolTip)
              }
          }
      
          Text {
              id: rText
              anchors {
                  right: root.right
                  top: root.top
              }
              verticalAlignment: Text.AlignTop
              horizontalAlignment: Text.AlignRight
              font.pointSize: 16//root.fontPointSize
              text: {
                  root.value
                      ? String(setPrecision (rad2Deg (root.value), percision))
                      : "N/A"
              }
              width: root.textWidth
          }
      
          Label {
              id: rLabel
              text: root.label
              anchors.top: rText.bottom
              anchors.right: rText.right
              verticalAlignment: Text.AlignTop
              horizontalAlignment: Text.AlignRight
              font.pointSize: root.fontPointSize
              color: root.labelColor
          }
      
          Image {
              id: rImage
              anchors {
                  right: rLabel.left
                  top: rLabel.top
              }
              visible:  false
              source: "/stale.svg"
              ToolTip.visible: (rImage.visible && ToolTip.text.length) > 0 && ma.containsMouse
      
              MouseArea {
                  id: ma
                  anchors.fill: parent
                  hoverEnabled: true
                  onContainsMouseChanged: checkStaleStatus()
              }
          }
      
          Timer {
              interval: 250
              running: true
              repeat: true
              onTriggered: simonP.stale()
          }
      }
      

      Usage:

      import QtQuick 2.0
      import QtQuick.Controls 2.15
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Layouts 1.3
      import QtQuick.Extras 1.4
      import QtQuick.Window 2.12
      import SimonsCPP 1.0
      
      Window {
          id: root
          visible: true
          width: 640
          height: 480
          title: qsTr("Playground / Staging Arena")
      
          StaleDataModel {
              id: hdg
              label: "HDG"
              anchors {
                  right: parent.right
                  top: parent.top
              }
              fontPointSize: parent.width < 150 ? 30 : 16
              textWidth: 256
          }
      
          Button {
              id: btnData
              text: "Click Me..."
              anchors {
                  left: parent.left
                  top: parent.top
              }
              onClicked: {
                  hdg.setValue(parseFloat(hdg.value) + 0.01);
              }
          }
      }
      

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #2

        @SPlatten said in Correct way to do test for null in QML?:

                if ( root.anchorHorzCenter !== 0 ) {
                    return root.anchorHorzCenter
                }
        

        this is not a function why return?
        anchors.horizontalCenter: {
        if ( root.anchorHorzCenter !== 0 ) {
        return root.anchorHorzCenter
        } you need else as well?
        }

        1 Reply Last reply
        0
        • SPlattenS SPlatten

          In QML I have a property:

              id: root
              property var anchorHorzCenter: parent.anchors.horizontalCenter
              property var anchorTop: parent.anchors.top
          

          Usage in the same QML:

              Text {
                  id: rText
                  anchors.horizontalCenter: {
                      if ( root.anchorHorzCenter !== 0 ) {
                          return root.anchorHorzCenter
                      }
                  }
                  anchors.top: {
                      if ( root.anchorTop !== 0 ) {
                          return root.anchorTop
                      }
                  }
          

          This can't correct because when I run the application I get:

          qrc:/StaleDataModel.qml:41:5: QML QQuickText: Cannot anchor to a null item. qrc:/StaleDataModel.qml: 41
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @SPlatten

          anchors.horizontalCenter:  root.anchorHorzCenter ? root.anchorHorzCenter : undefined
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #4

            @J-Hilk said in Correct way to do test for null in QML?:

            anchors.horizontalCenter: root.anchorHorzCenter ? root.anchorHorzCenter : undefined

            I addition to this, if you are running 5.15 and above, you can do nullish operator too:

            anchors.horizontalCenter:  root.anchorHorzCenter ?? undefined
            

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by SPlatten
              #5

              This is the QML I have:

              import QtQuick 2.0
              import QtQuick.Controls 2.15
              import QtQuick.Controls.Styles 1.4
              import QtQuick.Layouts 1.3
              import QtQuick.Extras 1.4
              import QtQuick.Window 2.12
              import SimonsCPP 1.0
              
              Item {
                  id: root
                  property var fontPointSize: 30
                  property string label
                  property var percision: 4
                  property real textWidth: 80
                  property var value: simonP.value
              
                  function checkStaleStatus(strToolTip) {
                      if ( typeof strToolTip != "string" ) {
                          simonP.stale()
                          return
                      }
                      rImage.ToolTip.text = strToolTip
                      rImage.visible = (strToolTip.length > 0)
              
                      if ( rImage.visible === true ) {
                          rText.color = simonP.strStale()
                      } else {
                          rText.color = simonP.strNormal()
                      }
                  }
                  function rad2Deg(rad_) {
                    return rad_ * 180.0 /  Math.PI;
                  }
                  function setPrecision (val_, sigDigit_) {
                      var precision = Math.abs (val_) >= 1000 ? sigDigit_ - 4 :
                      Math.abs (val_) >= 100 ? sigDigit_ - 3 :
                      Math.abs (val_) >= 10 ? sigDigit_ - 2 :
                      2;
              
                    if (precision < 0)
                      precision = 0;
              
                    return val_.toFixed (precision);
                  }
                  function setValue(newValue) {
                      simonP.setValue(newValue)
                      rText.text = setPrecision (newValue, percision)
                  }
              
                  StaleData {
                      id: simonP
                      onValueChanged: {
                          checkStaleStatus(strToolTip)
                      }
                  }
              
                  Text {
                      id: rText
                      anchors {
                          top: parent.top
                          right: parent.right
                      }
                      verticalAlignment: Text.AlignTop
                      horizontalAlignment: Text.AlignLeft
                      font.pointSize: root.fontPointSize
                      text: { root.value
                              ? String(setPrecision (rad2Deg (root.value), percision))
                              : "N/A"
                      }       
                      width: root.textWidth
                  }
              
                  Label {
                      id: rLabel
                      text: root.label
                      anchors.top: rText.bottom
                      verticalAlignment: Text.AlignTop
                      horizontalAlignment: Text.AlignLeft
                      font.pointSize: root.fontPointSize
                      width: parent.width
                      color: "#FF000000"
                  }
              
                  Timer {
                      interval: 250
                      running: true
                      repeat: true
                      onTriggered: simonP.stale()
                  }
              
                  Image {
                      id: rImage
                      visible:  false
                      anchors.left: rText.right
                      source: "/stale.svg"
                      ToolTip.visible: ToolTip.text.length > 0 && ma.containsMouse
              
                      MouseArea {
                          id: ma
                          anchors.fill: parent
                          hoverEnabled: true
                          onContainsMouseChanged: checkStaleStatus()
                      }
                  }
              }
              

              Usage:

              import QtQuick 2.0
              import QtQuick.Controls 2.15
              import QtQuick.Controls.Styles 1.4
              import QtQuick.Layouts 1.3
              import QtQuick.Extras 1.4
              import QtQuick.Window 2.12
              import SimonsCPP 1.0
              
              Window {
                  id: root
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Playground / Staging Arena")
              
                  StaleDataModel {
                      id:hdg
                      label: "HDG"
                      anchors {
                          left: parent.left
                          top: parent.top
                      }
                      fontPointSize: parent.width < 150 ? 30 : 16
                      textWidth: 256
                  }
              
                  Button {
                      id: btnData
                      text: "Click Me..."
                      anchors.top: hdg.anchors.bottom
                      onClicked: {
                          hdg.setValue(parseFloat(hdg.value) + 0.01);
                      }
                  }
              }
              

              What I want to achieve is the data over the label with the icon to the right of the data label. What I actually get is the icon under the label and no data showing at all.

              Kind Regards,
              Sy

              SPlattenS 1 Reply Last reply
              0
              • SPlattenS SPlatten

                This is the QML I have:

                import QtQuick 2.0
                import QtQuick.Controls 2.15
                import QtQuick.Controls.Styles 1.4
                import QtQuick.Layouts 1.3
                import QtQuick.Extras 1.4
                import QtQuick.Window 2.12
                import SimonsCPP 1.0
                
                Item {
                    id: root
                    property var fontPointSize: 30
                    property string label
                    property var percision: 4
                    property real textWidth: 80
                    property var value: simonP.value
                
                    function checkStaleStatus(strToolTip) {
                        if ( typeof strToolTip != "string" ) {
                            simonP.stale()
                            return
                        }
                        rImage.ToolTip.text = strToolTip
                        rImage.visible = (strToolTip.length > 0)
                
                        if ( rImage.visible === true ) {
                            rText.color = simonP.strStale()
                        } else {
                            rText.color = simonP.strNormal()
                        }
                    }
                    function rad2Deg(rad_) {
                      return rad_ * 180.0 /  Math.PI;
                    }
                    function setPrecision (val_, sigDigit_) {
                        var precision = Math.abs (val_) >= 1000 ? sigDigit_ - 4 :
                        Math.abs (val_) >= 100 ? sigDigit_ - 3 :
                        Math.abs (val_) >= 10 ? sigDigit_ - 2 :
                        2;
                
                      if (precision < 0)
                        precision = 0;
                
                      return val_.toFixed (precision);
                    }
                    function setValue(newValue) {
                        simonP.setValue(newValue)
                        rText.text = setPrecision (newValue, percision)
                    }
                
                    StaleData {
                        id: simonP
                        onValueChanged: {
                            checkStaleStatus(strToolTip)
                        }
                    }
                
                    Text {
                        id: rText
                        anchors {
                            top: parent.top
                            right: parent.right
                        }
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignLeft
                        font.pointSize: root.fontPointSize
                        text: { root.value
                                ? String(setPrecision (rad2Deg (root.value), percision))
                                : "N/A"
                        }       
                        width: root.textWidth
                    }
                
                    Label {
                        id: rLabel
                        text: root.label
                        anchors.top: rText.bottom
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignLeft
                        font.pointSize: root.fontPointSize
                        width: parent.width
                        color: "#FF000000"
                    }
                
                    Timer {
                        interval: 250
                        running: true
                        repeat: true
                        onTriggered: simonP.stale()
                    }
                
                    Image {
                        id: rImage
                        visible:  false
                        anchors.left: rText.right
                        source: "/stale.svg"
                        ToolTip.visible: ToolTip.text.length > 0 && ma.containsMouse
                
                        MouseArea {
                            id: ma
                            anchors.fill: parent
                            hoverEnabled: true
                            onContainsMouseChanged: checkStaleStatus()
                        }
                    }
                }
                

                Usage:

                import QtQuick 2.0
                import QtQuick.Controls 2.15
                import QtQuick.Controls.Styles 1.4
                import QtQuick.Layouts 1.3
                import QtQuick.Extras 1.4
                import QtQuick.Window 2.12
                import SimonsCPP 1.0
                
                Window {
                    id: root
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Playground / Staging Arena")
                
                    StaleDataModel {
                        id:hdg
                        label: "HDG"
                        anchors {
                            left: parent.left
                            top: parent.top
                        }
                        fontPointSize: parent.width < 150 ? 30 : 16
                        textWidth: 256
                    }
                
                    Button {
                        id: btnData
                        text: "Click Me..."
                        anchors.top: hdg.anchors.bottom
                        onClicked: {
                            hdg.setValue(parseFloat(hdg.value) + 0.01);
                        }
                    }
                }
                

                What I want to achieve is the data over the label with the icon to the right of the data label. What I actually get is the icon under the label and no data showing at all.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #6

                @SPlatten , SOLVED:

                StaleDataModel.qml:

                import QtQuick 2.0
                import QtQuick.Controls 2.15
                import QtQuick.Controls.Styles 1.4
                import QtQuick.Layouts 1.3
                import QtQuick.Extras 1.4
                import QtQuick.Window 2.12
                import SimonsCPP 1.0
                
                Item {
                    id: root
                    property var fontPointSize: 16
                    property string label
                    property string labelColor: "#FF000000"
                    property var percision: 4
                    property real textWidth: 80
                    property var value: simonP.value
                
                    function checkStaleStatus(strToolTip) {
                        if ( typeof strToolTip != "string" ) {
                            simonP.stale()
                            return
                        }
                        rImage.ToolTip.text = strToolTip
                        rImage.visible = (strToolTip.length > 0)
                
                        if ( rImage.visible === true ) {
                            rText.color = simonP.strStale()
                        } else {
                            rText.color = simonP.strNormal()
                        }
                    }
                    function rad2Deg(rad_) {
                      return rad_ * 180.0 /  Math.PI;
                    }
                    function setPrecision (val_, sigDigit_) {
                        var precision = Math.abs (val_) >= 1000 ? sigDigit_ - 4 :
                        Math.abs (val_) >= 100 ? sigDigit_ - 3 :
                        Math.abs (val_) >= 10 ? sigDigit_ - 2 :
                        2;
                
                      if (precision < 0)
                        precision = 0;
                
                      return val_.toFixed (precision);
                    }
                    function setValue(newValue) {
                        simonP.setValue(newValue)
                        rText.text = setPrecision (newValue, percision)
                    }
                
                    StaleData {
                        id: simonP
                        onValueChanged: {
                            checkStaleStatus(strToolTip)
                        }
                    }
                
                    Text {
                        id: rText
                        anchors {
                            right: root.right
                            top: root.top
                        }
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignRight
                        font.pointSize: 16//root.fontPointSize
                        text: {
                            root.value
                                ? String(setPrecision (rad2Deg (root.value), percision))
                                : "N/A"
                        }
                        width: root.textWidth
                    }
                
                    Label {
                        id: rLabel
                        text: root.label
                        anchors.top: rText.bottom
                        anchors.right: rText.right
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignRight
                        font.pointSize: root.fontPointSize
                        color: root.labelColor
                    }
                
                    Image {
                        id: rImage
                        anchors {
                            right: rLabel.left
                            top: rLabel.top
                        }
                        visible:  false
                        source: "/stale.svg"
                        ToolTip.visible: (rImage.visible && ToolTip.text.length) > 0 && ma.containsMouse
                
                        MouseArea {
                            id: ma
                            anchors.fill: parent
                            hoverEnabled: true
                            onContainsMouseChanged: checkStaleStatus()
                        }
                    }
                
                    Timer {
                        interval: 250
                        running: true
                        repeat: true
                        onTriggered: simonP.stale()
                    }
                }
                

                Usage:

                import QtQuick 2.0
                import QtQuick.Controls 2.15
                import QtQuick.Controls.Styles 1.4
                import QtQuick.Layouts 1.3
                import QtQuick.Extras 1.4
                import QtQuick.Window 2.12
                import SimonsCPP 1.0
                
                Window {
                    id: root
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Playground / Staging Arena")
                
                    StaleDataModel {
                        id: hdg
                        label: "HDG"
                        anchors {
                            right: parent.right
                            top: parent.top
                        }
                        fontPointSize: parent.width < 150 ? 30 : 16
                        textWidth: 256
                    }
                
                    Button {
                        id: btnData
                        text: "Click Me..."
                        anchors {
                            left: parent.left
                            top: parent.top
                        }
                        onClicked: {
                            hdg.setValue(parseFloat(hdg.value) + 0.01);
                        }
                    }
                }
                

                Kind Regards,
                Sy

                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