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. Save data from multiple Qt components scattered around multiple QML files

Save data from multiple Qt components scattered around multiple QML files

Scheduled Pinned Locked Moved Solved QML and Qt Quick
model bindingscopedesigndata modelsqml
9 Posts 2 Posters 2.0k 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.
  • I Offline
    I Offline
    ivarec
    wrote on 20 Jan 2019, 02:49 last edited by ivarec
    #1

    I would like to save the current "value" property of several components (e.g. a Slider) as a configuration profile when the user clicks on the Save button in my application. However, the Save button is in a different file than the rest of the components. How should I deal with this? Some solutions that I have thought:

    1. do a two-way binding between the "value" property of the component and a role in my model (by using a Binding)
    2. reference every component by ID, even if they are in different QML files, in the onClicked handler in my Save button and manually save the data to the model
    3. make onClicked in my Save button to emit a signal that will be "caught" in every QML file, making the components commit their value to my model

    Am I on the right track here?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 20 Jan 2019, 03:53 last edited by
      #2

      when the save button is clicked you should emit signal & set the value in appropriate objects is Qml components. Then component sholud take care saving using property binding. When model, is it a model as in model view framework. Or is it just a c++ object ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ivarec
        wrote on 20 Jan 2019, 12:22 last edited by
        #3

        My model is a C++ object based on QSqlTableModel.

        However, I think you got it backwards. I don’t want to SET the values of my QML componentes - I want to READ their values and store them in the model. Since they are in different files, I wonder what are the good practices regarding this scenario.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 20 Jan 2019, 13:33 last edited by
          #4

          2nd and 3rd option look same. All other QML components are created when you are in save button screen. So you should have id of object with you. When you click on the save button, value should be saved in particular index of the model. So this signal should be propagated to each and every component. They should take care of saving the data to appropriate index of the model. When you say QML components are they used as delegate objects ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          I 1 Reply Last reply 21 Jan 2019, 04:13
          0
          • D dheerendra
            20 Jan 2019, 13:33

            2nd and 3rd option look same. All other QML components are created when you are in save button screen. So you should have id of object with you. When you click on the save button, value should be saved in particular index of the model. So this signal should be propagated to each and every component. They should take care of saving the data to appropriate index of the model. When you say QML components are they used as delegate objects ?

            I Offline
            I Offline
            ivarec
            wrote on 21 Jan 2019, 04:13 last edited by
            #5

            @dheerendra I don’t have the IDs with me. They are in another scope. My Save button is in a side panel that shares the same parent as the QML components, but trying to access them by ID fails. The only thing that seems to work is the two way binding, but that looks overly complicated. Data models in QML are hard :)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 21 Jan 2019, 05:23 last edited by
              #6

              Any sample to demonstrate your stuff ? I'm wondering why they are not accessible.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              I 1 Reply Last reply 21 Jan 2019, 12:39
              0
              • D dheerendra
                21 Jan 2019, 05:23

                Any sample to demonstrate your stuff ? I'm wondering why they are not accessible.

                I Offline
                I Offline
                ivarec
                wrote on 21 Jan 2019, 12:39 last edited by
                #7

                @dheerendra Will try to set a minimal example, but I have a StackView where each view is a different QML file. I can't get the ID of a component in view A from view B.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on 21 Jan 2019, 12:42 last edited by
                  #8

                  If it is stack view you can use the get(index) method to get the object and call appropriate methods. Or is it something else ?

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ivarec
                    wrote on 27 Jan 2019, 01:28 last edited by
                    #9

                    I went with the Binding solution in the end :)

                    1 Reply Last reply
                    0

                    9/9

                    27 Jan 2019, 01:28

                    • Login

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