Correct way to do test for null in QML?
-
In QML I have a property:
id: root property var anchorHorzCenter: parent.anchors.horizontalCenter property var anchorTop: parent.anchors.topUsage 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 -
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.
@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); } } } -
@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?
} -
In QML I have a property:
id: root property var anchorHorzCenter: parent.anchors.horizontalCenter property var anchorTop: parent.anchors.topUsage 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-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 -
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.
-
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.
@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); } } }