Move object to another qml
-
wrote 26 days ago last edited by
Hello,
I'm writing a data viewer in QML, where the data is provided by C++ classes.
The base class is:
class DataCore : public QAbstractTableModel
And I have several derived classes, such as:class SerialData : public DataCore
class SCVData : public DataCore
In QML, I have a MainPage.qml, which contains a Loader. This loader is responsible for loading different QML pages.There are two types of QML pages:
Configuration pages – used to configure data sources (e.g., SerialDataConfig.qml for configuring serial port settings like baud rate, parity, etc., or SCVDataConfig.qml for selecting a file).
Viewer pages – used for displaying the data.
In MainPage.qml, I have: property var modelData: null
This property is used as a data source in the viewer pages.I want to set mainPage.modelData from within a configuration page like SerialDataConfig.qml.
My current workaround is to use Qt.createQmlObject() inside a JavaScript function in MainPage.qml.However, I would prefer to instantiate the object directly in SerialDataConfig.qml like this:
SerialData { id: dataModel baudRate: inputBox.text ... } And then assign this object to mainPage.modelData. The problem is, I don’t know how to deep copy this object to mainPage.modelData, or how to properly change its ownership. Maybe there is a better approach. Or maybe my overall design is flawed.
-
wrote 26 days ago last edited by
You must register your SerialData type with QML in C++ using
qmlRegisterType<SerialData>("YourModule", 1, 0, "SerialData");
Item { id: mainPage property var modelData: null Loader { id: pageLoader source: "SerialDataConfig.qml" onLoaded: { if (pageLoader.item && pageLoader.item.dataConfigured) { pageLoader.item.dataConfigured.connect(function(dataModel) { mainPage.modelData = dataModel // Optionally load viewer page here }) } } } }
-
wrote 24 days ago last edited by
I don't think you answered my question
-
wrote 24 days ago last edited by CassD 5 Jan 2025, 19:57
Improperly explained problems lead to bad answers. I'm not sure I properly understand what you are asking so I probably won't answer it either and each time I read your question I understand a completely different thing of the previous read. I've read five times and I'm still unsure about what I should understand.
If I'm understanding it right, you might be willing to instanciate an object outside a Loader, but be able to access it within it ?
if yes then you need to use a binding.Item { id: rootItem SerialData { id: serialData } Loader { id: loader source: someCondition ? "page1.qml" : "page2.qml" } Binding { target: loader property: "serialData" // the property name as it will be available inside the loader WITHIN PARENTHESES value: data } }
and inside your loader you can access your property classically via
rootItem.serialData
other option : you want to instantiate your SerialData component inside some component and make it accessible to it's parent. Then you can declare it as a property inside the component that instanciates it and thus make it available to the parent component. Doing this for data properties is fine, but injecting out an object is bad architecture. That's a very bad choice. It is much cleaner to create it in the main page and inject it into the subpages rather than creating it in a subpage and injecting it out to the main page and back to the siblings.
1/4