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. Passing model data from delegate Loader to component in TableView object.
Forum Updated to NodeBB v4.3 + New Features

Passing model data from delegate Loader to component in TableView object.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 4 Posters 763 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jebediahcrunk
    wrote on last edited by jebediahcrunk
    #1

    Hi,

    Firstly, I hope that this post conforms to the rules of this forum as this is my first post. I have a QAbstractTableModel that I am passing into my qml file. To then visualise this object (with a 2d grid), I am using a delegate loader and attempting to pass in the model from the loader to the component although i cant even manage to pass in model.gateType (just a QString) with the following code:

    TableView {
                    id: circuitGrid
        //this is where i pass in the model 
                    model: qgui && qgui.gateModel ? qgui.gateModel : null
    
                    Component {
                        id: gateDelegate
                        GateIcon {
                            gateType : delegateLoader.type
           
                            implicitWidth: 40
                            implicitHeight: 40
                    }
    
                    delegate: Item {
                        implicitWidth: 40
                        implicitHeight: 40
    
                        Loader {
                            id: delegateLoader
                            anchors.fill: parent
                            property string type : model.gateType
                            sourceComponent: gateDelegate
                            
                        }
                    }
                }
    

    I am sure there is a much simpler/better way to do this but i have been trying for a while now. The code gives the following error:

    ReferenceError: delegateLoader is not defined
    
    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      Can you try:
      gateType : model.gateType

      be aware of id: delegateLoader is same for all delegates and not unique.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jebediahcrunk
        wrote on last edited by
        #3

        Hi thanks,

        Unfortunately that still gives
        ReferenceError: model is not defined

        JoeCFDJ 1 Reply Last reply
        0
        • J jebediahcrunk

          Hi thanks,

          Unfortunately that still gives
          ReferenceError: model is not defined

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          @jebediahcrunk Is } for GateIcon missing?

                          Component {
                              id: gateDelegate
                              GateIcon {
                                  gateType : delegateLoader.type
                 
                                  implicitWidth: 40
                                  implicitHeight: 40
                              }
                        }
          
          1 Reply Last reply
          0
          • J Offline
            J Offline
            jebediahcrunk
            wrote on last edited by
            #5

            Sorry, i took some parts out of my actual code to make it a bit more readable so accidentally deleted that in the process. That is not the issue though.

            JoeCFDJ 1 Reply Last reply
            0
            • J jebediahcrunk

              Sorry, i took some parts out of my actual code to make it a bit more readable so accidentally deleted that in the process. That is not the issue though.

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #6

              @jebediahcrunk I did similar thing.
              model is visible for component and gateType : model.gateType should work.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Bob64
                wrote on last edited by Bob64
                #7

                I do something like this in my application. Loader introduces some not necessarily intuitive behaviour about scoping of properties. I think you have accounted for this by introducing your type property at the loader level as a means of passing the model's gateType to your loaded component.

                However, in my code at least, I do not refer to the loader in my loaded component (I do not even give the loader an ID). Instead I simply access the properties that are defined at the loader level because these now exist in the scope of the loaded component.

                In your case, that would mean changing to this:

                GateIcon {
                    gateType : type // this is the property defined in `Loader`
                    ...
                }
                
                1 Reply Last reply
                0
                • GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on last edited by
                  #8

                  @jebediahcrunk : is the Loader needed here?

                  What is discussed here is sharing data via context properties, a system that is not recommended anymore.

                  B 1 Reply Last reply
                  0
                  • GrecKoG GrecKo

                    @jebediahcrunk : is the Loader needed here?

                    What is discussed here is sharing data via context properties, a system that is not recommended anymore.

                    B Offline
                    B Offline
                    Bob64
                    wrote on last edited by
                    #9

                    @GrecKo said in Passing model data from delegate Loader to component in TableView object.:

                    @jebediahcrunk : is the Loader needed here?

                    What is discussed here is sharing data via context properties, a system that is not recommended anymore.

                    That's interesting. What would be recommended now?

                    I use Loader for a case where I have heterogeneous components shown in a ListView. I am aware of DelegateChooser as another way of doing this in theory, but I saw some weird behaviour when I tried to use it. I forget the details - I need to have another go sometime - but things like properties such as the model index that should have been accessible were sometimes not available in the delegates. Also, the documentation is unclear as to whether it should even be expected to work with a ListView. (Is ListView "internally based on a DelegateModel"? How am I supposed to know this?)

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #10

                      That's interesting. What would be recommended now?

                      DelegateChooser

                      Is ListView "internally based on a DelegateModel"? How am I supposed to know this?

                      Yes. Good question 😬 reading the code can answer that but that's hardly practical.
                      In practice view taking QAbstractItemModel but also QList, JS arrays are based on DelegateModel.
                      This is the case for ListView, TableView, Repeater, ...

                      B 1 Reply Last reply
                      1
                      • GrecKoG GrecKo

                        That's interesting. What would be recommended now?

                        DelegateChooser

                        Is ListView "internally based on a DelegateModel"? How am I supposed to know this?

                        Yes. Good question 😬 reading the code can answer that but that's hardly practical.
                        In practice view taking QAbstractItemModel but also QList, JS arrays are based on DelegateModel.
                        This is the case for ListView, TableView, Repeater, ...

                        B Offline
                        B Offline
                        Bob64
                        wrote on last edited by
                        #11

                        @GrecKo OK, thanks. I did actually want to switch to DelegateChooser so I will try it again. And good to know about its applicability.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jebediahcrunk
                          wrote on last edited by
                          #12

                          @Bob64 Thank you, just using 'type' worked! I knew it would be something simple like this. And @GrecKo thanks for the tip, that looks like a better solution so ill definitely look into it.

                          1 Reply Last reply
                          0

                          • Login

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