Ongoing problem, trying to create manager in singleton...
-
The issues:
QQmlApplicationEngine failed to load component qrc:/stage.qml:135:5: Type DataModel unavailable qrc:/stage.qml: 135 <Unknown File>:138:5: Element is not creatable.stage.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 Window { property string dptTag: "DPT" property string hdgTag: "HDG" property bool dptTestDataEnabled : false property bool hdgTestDataEnabled : false id: root visible: true width: 640 height: 480 title: qsTr("Playground / Staging Arena") Button { id: btnDptData text: "DPT Click Me..." anchors { left: parent.left top: parent.top } onClicked: { var value = dpt.getValue() if ( typeof value === "undefined" ) { value = 0.0 } else { value = Number(value) } if ( dpt.setValue(value + 0.01) === true ) { btnClearDevFailure.clicked() } } } Button { id: btnDptTestData text: (dptTestDataEnabled == true) ? "Stop Test Data" : "Start Test Data" anchors { left: btnDptData.right top: btnDptData.top leftMargin: 4 } onClicked: { dptTestDataEnabled = (dptTestDataEnabled == true) ? false : true var api = dpt.api api.setTestData(dptTestDataEnabled) btnClearDevFailure.clicked() } } Button { id: btnHdgData text: "HDG Click Me..." anchors { left: btnDptData.left top: btnDptData.bottom topMargin: 4 } onClicked: { var value = hdg.getValue() if ( typeof value === "undefined" ) { value = 0.0 } else { value = Number(value) } if ( hdg.setValue(value + 0.01) === true ) { btnClearDevFailure.clicked() } } } Button { id: btnHdgTestData text: (hdgTestDataEnabled == true) ? "Stop Test Data" : "Start Test Data" anchors { left: btnHdgData.right top: btnHdgData.top leftMargin: 4 } onClicked: { hdgTestDataEnabled = (hdgTestDataEnabled == true) ? false : true var api = hdg.api api.setTestData(hdgTestDataEnabled) btnClearDevFailure.clicked() } } Button { id: btnDeviceFailure text: "Set Device Failure" enabled: !btnClearDevFailure.enabled anchors { left: btnDptData.left top: btnHdgData.bottom topMargin: 4 } onClicked: { if ( dptTestDataEnabled == true ) { btnDptTestData.clicked() } if ( hdgTestDataEnabled == true ) { btnHdgTestData.clicked() } var api = dpt.api api.setInterfaceStatus(false) if ( dptTestDataEnabled == true ) { btnDptTestData.clicked() } api = hdg.api api.setInterfaceStatus(false) if ( hdgTestDataEnabled == true ) { btnHdgTestData.clicked() } btnDeviceFailure.enabled = false } } Button { id: btnClearDevFailure text: "Clr Device Failure" enabled: !btnDeviceFailure.enabled anchors { left: btnDeviceFailure.right top: btnDeviceFailure.top leftMargin: 4 } onClicked: { var api = dpt.api api.setInterfaceStatus(true) api = hdg.api api.setInterfaceStatus(true) btnDeviceFailure.enabled = true } } DataModel { id: dpt anchors { right: parent.right top: parent.top } dataRef: "testData.testLink" device: "simulation" dataTag: dptTag logData: true ptszFont: 18 strDefault: "N/A" strLabel: dptTag strUnits: "M" textWidth: 256 } DataModel { id: hdg anchors { right: parent.right top: parent.top topMargin: 80 } dataTag: hdgTag device: "simulation" logData: true ptszFont: dpt.ptszFont strDefault: "N/A" strLabel: hdgTag strUnits: "\xB0" textWidth: dpt.textWidth } }DataModel.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 Qt.DMngrSingleton 1.0 Item { id: root readonly property alias api : datahlpr property int someValue : DataMngr.someProperty property string argbData : "#FF000000" property string argbLabel : argbData property string argbStale : "#FF777777" property string argbUnits : argbData property bool logData : false readonly property string cstrInvalid : "qrc:/images/invalid.svg" readonly property string cstrStale : "qrc:/images/stale.svg" readonly property string cstrLinkFail : "qrc:/images/link-fail.svg" property bool blnValid : false property var dataRef property var dataTag property string device property int intDecimalPlaces : 4 property int ptszFont : 14 property int ptszData : 16 property int ptszLabel : 10 property int ptszUnits : 14 property string strDefault : "" property string strLabel : "" property string strUnits : "" property double textWidth : 80 property double timeout : 5000 property double timestamp Component.onCompleted: { console.log("HELLO FROM DataModel.qml") console.log(DataMngr.doSomething()) if ( typeof root.dataTag === "string" ) { api.dataTag = root.dataTag } if ( typeof root.dataRef !== "undefined" ) { api.dataRef = root.dataRef } } /* onDeviceChanged: { api.device = root.device } function getValue(value, blnFormatOnly) { if ( typeof value === "undefined" && api.valid() ) { value = api.value } value = Number(value).toFixed(root.intDecimalPlaces) if ( typeof blnFormatOnly !== "undefined" ) { logMessage("getValue, typeof value: " + (typeof value) + ", value: " + value) } return value } function logMessage(strMsg) { if ( logData !== true || typeof strMsg !== "string" || strMsg.length === 0 ) { return } const dtNow = new Date() console.log(dtNow.toLocaleString() + " " + root.strLabel + " " + strMsg) } function setValue(newValue) { if ( api.value !== newValue ) { api.value = newValue } } Label { id: rUnits anchors.right: root.right anchors.top: root.top color: root.argbUnits font.pointSize: root.ptszUnits horizontalAlignment: Text.AlignRight verticalAlignment: Text.AlignTop text: { root.strUnits ? String(root.strUnits) : "" } visible: root.strUnits && root.strUnits.length > 0 } Text { id: rText anchors { right: root.right rightMargin: rUnits.visible ? 24 : 0 top: root.top } color: root.argbData font.pointSize: root.ptszData horizontalAlignment: Text.AlignRight text: root.strDefault verticalAlignment: Text.AlignTop width: root.textWidth } Label { id: rLabel anchors.right: rText.right anchors.top: rText.bottom color: root.argbLabel font.pointSize: root.ptszLabel horizontalAlignment: Text.AlignRight text: root.strLabel verticalAlignment: Text.AlignTop } Image { id: rImage visible: true anchors { right: rLabel.left verticalCenter: rLabel.verticalCenter verticalCenterOffset: 5 } MouseArea { id: ma anchors.fill: parent hoverEnabled: true onContainsMouseChanged: { api.stale() } } } Timer { interval: 250 running: true repeat: true onTriggered: { api.stale() } } */ DataMngr { id: datahlpr /* onDataChanged: { rText.text = root.getValue(dblData_, true) } onInterface: { logMessage(", Interface Status: " + blnState_) } onToolTip: { if ( api.valid() !== true ) { return } var blnVisible = (typeof strToolTip_ === "string" && strToolTip_.length > 0) if ( blnVisible === true ) { var strTagName = root.strLabel rText.color = root.argbStale rImage.ToolTip.text = strToolTip_ if ( api.valid(root.strLabel) !== true ) { rImage.source = cstrInvalid } else if (api.interfaceStatus() === true) { rImage.source = cstrStale } else { rImage.source = cstrLinkFail } } else { rText.color = root.argbData } rImage.ToolTip.visible = blnVisible && ma.containsMouse rImage.visible = blnVisible } */ } }The singleton class:
class staledataSingleton_t; typedef staledataSingleton_t DataMngrSingleton; //For QML acccess class staledataSingleton_t : public QObject { Q_OBJECT Q_PROPERTY(int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged) public: explicit staledataSingleton_t(QObject* pParent = nullptr) : QObject(pParent) {} Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; } int someProperty() const { return m_someProperty; } void setSomeProperty(int val) { if (m_someProperty != val) { m_someProperty = val; emit somePropertyChanged(val); } } signals: void somePropertyChanged(int newValue); private: int m_someProperty = 0; };Registration:
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QScopedPointer<DataMngrSingleton> DMngrSingleton(new DataMngrSingleton); QQmlApplicationEngine engine; qmlRegisterSingletonInstance("Qt.DMngrSingleton", 1, 0, "DataMngr", DMngrSingleton.get());This is very much a work in progress.
-
The issues:
QQmlApplicationEngine failed to load component qrc:/stage.qml:135:5: Type DataModel unavailable qrc:/stage.qml: 135 <Unknown File>:138:5: Element is not creatable.stage.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 Window { property string dptTag: "DPT" property string hdgTag: "HDG" property bool dptTestDataEnabled : false property bool hdgTestDataEnabled : false id: root visible: true width: 640 height: 480 title: qsTr("Playground / Staging Arena") Button { id: btnDptData text: "DPT Click Me..." anchors { left: parent.left top: parent.top } onClicked: { var value = dpt.getValue() if ( typeof value === "undefined" ) { value = 0.0 } else { value = Number(value) } if ( dpt.setValue(value + 0.01) === true ) { btnClearDevFailure.clicked() } } } Button { id: btnDptTestData text: (dptTestDataEnabled == true) ? "Stop Test Data" : "Start Test Data" anchors { left: btnDptData.right top: btnDptData.top leftMargin: 4 } onClicked: { dptTestDataEnabled = (dptTestDataEnabled == true) ? false : true var api = dpt.api api.setTestData(dptTestDataEnabled) btnClearDevFailure.clicked() } } Button { id: btnHdgData text: "HDG Click Me..." anchors { left: btnDptData.left top: btnDptData.bottom topMargin: 4 } onClicked: { var value = hdg.getValue() if ( typeof value === "undefined" ) { value = 0.0 } else { value = Number(value) } if ( hdg.setValue(value + 0.01) === true ) { btnClearDevFailure.clicked() } } } Button { id: btnHdgTestData text: (hdgTestDataEnabled == true) ? "Stop Test Data" : "Start Test Data" anchors { left: btnHdgData.right top: btnHdgData.top leftMargin: 4 } onClicked: { hdgTestDataEnabled = (hdgTestDataEnabled == true) ? false : true var api = hdg.api api.setTestData(hdgTestDataEnabled) btnClearDevFailure.clicked() } } Button { id: btnDeviceFailure text: "Set Device Failure" enabled: !btnClearDevFailure.enabled anchors { left: btnDptData.left top: btnHdgData.bottom topMargin: 4 } onClicked: { if ( dptTestDataEnabled == true ) { btnDptTestData.clicked() } if ( hdgTestDataEnabled == true ) { btnHdgTestData.clicked() } var api = dpt.api api.setInterfaceStatus(false) if ( dptTestDataEnabled == true ) { btnDptTestData.clicked() } api = hdg.api api.setInterfaceStatus(false) if ( hdgTestDataEnabled == true ) { btnHdgTestData.clicked() } btnDeviceFailure.enabled = false } } Button { id: btnClearDevFailure text: "Clr Device Failure" enabled: !btnDeviceFailure.enabled anchors { left: btnDeviceFailure.right top: btnDeviceFailure.top leftMargin: 4 } onClicked: { var api = dpt.api api.setInterfaceStatus(true) api = hdg.api api.setInterfaceStatus(true) btnDeviceFailure.enabled = true } } DataModel { id: dpt anchors { right: parent.right top: parent.top } dataRef: "testData.testLink" device: "simulation" dataTag: dptTag logData: true ptszFont: 18 strDefault: "N/A" strLabel: dptTag strUnits: "M" textWidth: 256 } DataModel { id: hdg anchors { right: parent.right top: parent.top topMargin: 80 } dataTag: hdgTag device: "simulation" logData: true ptszFont: dpt.ptszFont strDefault: "N/A" strLabel: hdgTag strUnits: "\xB0" textWidth: dpt.textWidth } }DataModel.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 Qt.DMngrSingleton 1.0 Item { id: root readonly property alias api : datahlpr property int someValue : DataMngr.someProperty property string argbData : "#FF000000" property string argbLabel : argbData property string argbStale : "#FF777777" property string argbUnits : argbData property bool logData : false readonly property string cstrInvalid : "qrc:/images/invalid.svg" readonly property string cstrStale : "qrc:/images/stale.svg" readonly property string cstrLinkFail : "qrc:/images/link-fail.svg" property bool blnValid : false property var dataRef property var dataTag property string device property int intDecimalPlaces : 4 property int ptszFont : 14 property int ptszData : 16 property int ptszLabel : 10 property int ptszUnits : 14 property string strDefault : "" property string strLabel : "" property string strUnits : "" property double textWidth : 80 property double timeout : 5000 property double timestamp Component.onCompleted: { console.log("HELLO FROM DataModel.qml") console.log(DataMngr.doSomething()) if ( typeof root.dataTag === "string" ) { api.dataTag = root.dataTag } if ( typeof root.dataRef !== "undefined" ) { api.dataRef = root.dataRef } } /* onDeviceChanged: { api.device = root.device } function getValue(value, blnFormatOnly) { if ( typeof value === "undefined" && api.valid() ) { value = api.value } value = Number(value).toFixed(root.intDecimalPlaces) if ( typeof blnFormatOnly !== "undefined" ) { logMessage("getValue, typeof value: " + (typeof value) + ", value: " + value) } return value } function logMessage(strMsg) { if ( logData !== true || typeof strMsg !== "string" || strMsg.length === 0 ) { return } const dtNow = new Date() console.log(dtNow.toLocaleString() + " " + root.strLabel + " " + strMsg) } function setValue(newValue) { if ( api.value !== newValue ) { api.value = newValue } } Label { id: rUnits anchors.right: root.right anchors.top: root.top color: root.argbUnits font.pointSize: root.ptszUnits horizontalAlignment: Text.AlignRight verticalAlignment: Text.AlignTop text: { root.strUnits ? String(root.strUnits) : "" } visible: root.strUnits && root.strUnits.length > 0 } Text { id: rText anchors { right: root.right rightMargin: rUnits.visible ? 24 : 0 top: root.top } color: root.argbData font.pointSize: root.ptszData horizontalAlignment: Text.AlignRight text: root.strDefault verticalAlignment: Text.AlignTop width: root.textWidth } Label { id: rLabel anchors.right: rText.right anchors.top: rText.bottom color: root.argbLabel font.pointSize: root.ptszLabel horizontalAlignment: Text.AlignRight text: root.strLabel verticalAlignment: Text.AlignTop } Image { id: rImage visible: true anchors { right: rLabel.left verticalCenter: rLabel.verticalCenter verticalCenterOffset: 5 } MouseArea { id: ma anchors.fill: parent hoverEnabled: true onContainsMouseChanged: { api.stale() } } } Timer { interval: 250 running: true repeat: true onTriggered: { api.stale() } } */ DataMngr { id: datahlpr /* onDataChanged: { rText.text = root.getValue(dblData_, true) } onInterface: { logMessage(", Interface Status: " + blnState_) } onToolTip: { if ( api.valid() !== true ) { return } var blnVisible = (typeof strToolTip_ === "string" && strToolTip_.length > 0) if ( blnVisible === true ) { var strTagName = root.strLabel rText.color = root.argbStale rImage.ToolTip.text = strToolTip_ if ( api.valid(root.strLabel) !== true ) { rImage.source = cstrInvalid } else if (api.interfaceStatus() === true) { rImage.source = cstrStale } else { rImage.source = cstrLinkFail } } else { rText.color = root.argbData } rImage.ToolTip.visible = blnVisible && ma.containsMouse rImage.visible = blnVisible } */ } }The singleton class:
class staledataSingleton_t; typedef staledataSingleton_t DataMngrSingleton; //For QML acccess class staledataSingleton_t : public QObject { Q_OBJECT Q_PROPERTY(int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged) public: explicit staledataSingleton_t(QObject* pParent = nullptr) : QObject(pParent) {} Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; } int someProperty() const { return m_someProperty; } void setSomeProperty(int val) { if (m_someProperty != val) { m_someProperty = val; emit somePropertyChanged(val); } } signals: void somePropertyChanged(int newValue); private: int m_someProperty = 0; };Registration:
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QScopedPointer<DataMngrSingleton> DMngrSingleton(new DataMngrSingleton); QQmlApplicationEngine engine; qmlRegisterSingletonInstance("Qt.DMngrSingleton", 1, 0, "DataMngr", DMngrSingleton.get());This is very much a work in progress.
@SPlatten said in Ongoing problem, trying to create manager in singleton...:
you have the singleton instance
DataMngrand this oneDataMngr { id: datahlprthis is a naming conflict,
-
@SPlatten said in Ongoing problem, trying to create manager in singleton...:
you have the singleton instance
DataMngrand this oneDataMngr { id: datahlprthis is a naming conflict,
-
@SPlatten said in Ongoing problem, trying to create manager in singleton...:
you have the singleton instance
DataMngrand this oneDataMngr { id: datahlprthis is a naming conflict,
@J-Hilk , is there a way to get the id of the instance of the QML document being created?
For example, I have DataModel.qml which internally has an id of root. Another QML document has two instances of this, each with an unique id for the instance.
Is there a way in the DataModel.qml to get the id that instantiates DataModel.qml ? or do I have to create another property and reference that?
-
@J-Hilk , is there a way to get the id of the instance of the QML document being created?
For example, I have DataModel.qml which internally has an id of root. Another QML document has two instances of this, each with an unique id for the instance.
Is there a way in the DataModel.qml to get the id that instantiates DataModel.qml ? or do I have to create another property and reference that?
@SPlatten said in Ongoing problem, trying to create manager in singleton...:
or do I have to create another property and reference that
this one, ID is only available in the actual qml file
from the documentation:
An object can be referred to by its id from anywhere within the component scope in which it is declared. Therefore, an id value must always be unique within its component scope.
...
Once an object instance is created, the value of its id attribute cannot be changed. While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it
-
@SPlatten said in Ongoing problem, trying to create manager in singleton...:
or do I have to create another property and reference that
this one, ID is only available in the actual qml file
from the documentation:
An object can be referred to by its id from anywhere within the component scope in which it is declared. Therefore, an id value must always be unique within its component scope.
...
Once an object instance is created, the value of its id attribute cannot be changed. While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it
-
@J-Hilk , so how can I determine in the singleton what the id of the object that is using it?