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. Move object to another qml
Forum Update on Monday, May 27th 2025

Move object to another qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 119 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.
  • P Offline
    P Offline
    poucz
    wrote 26 days ago last edited by
    #1

    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.
    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rangantha B V
      wrote 26 days ago last edited by
      #2

      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
                      })
                  }
              }
          }
      }
      
      
      1 Reply Last reply
      0
      • P Offline
        P Offline
        poucz
        wrote 24 days ago last edited by
        #3

        I don't think you answered my question

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CassD
          wrote 24 days ago last edited by CassD 5 Jan 2025, 19:57
          #4

          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 Reply Last reply
          0

          1/4

          30 Apr 2025, 06:48

          • Login

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