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. Windows {} COmponent.onCompleted is too late, how to trigger script as a first thing?
Forum Updated to NodeBB v4.3 + New Features

Windows {} COmponent.onCompleted is too late, how to trigger script as a first thing?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 445 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    Hello all,

    toigh situation...

    at the beginning i declare a variable variant: [] which is empty, and then i need to run a script which will populate this variable one by one (via array.push()).
    Please note that this is must... for my reasons i can not use variable variant: function toPopulate()

    so i have tried:

    Window {
    property variant gridValue: []
    Component.onCompleted: JS.populateGrid()
    ....
    }
    

    which works fine... but i have a problem that under window, i have something like this:
    ```
    GridLayout {

                        anchors.fill: parent
    
                        Repeater {
                            model: 9
                            delegate: Rectangle {
    
                                Label {
                                    text: gridValue[index]['value']    // the problem is, that at this moment the JS.populateGrid did not happen yet
                                }
                            }
                        }
                    }
    
    
    so im looking for any idea how to force the JS.populateGrid() before the label texts are being printed...
    
    I tried even some ugly solution like this (which obviously did not help):
    

    Window {
    property variant gridValue: []
    property bool triggerPopulateGrid: false
    onTriggerPopulateGridChanged: { console.log("trying to trigger, but does not work, wont even compile (ninja error)"); JS.populateGrid(); }
    triggerPopulateGrid: true
    ....
    }

    GrecKoG 1 Reply Last reply
    0
    • S shokarta

      Hello all,

      toigh situation...

      at the beginning i declare a variable variant: [] which is empty, and then i need to run a script which will populate this variable one by one (via array.push()).
      Please note that this is must... for my reasons i can not use variable variant: function toPopulate()

      so i have tried:

      Window {
      property variant gridValue: []
      Component.onCompleted: JS.populateGrid()
      ....
      }
      

      which works fine... but i have a problem that under window, i have something like this:
      ```
      GridLayout {

                          anchors.fill: parent
      
                          Repeater {
                              model: 9
                              delegate: Rectangle {
      
                                  Label {
                                      text: gridValue[index]['value']    // the problem is, that at this moment the JS.populateGrid did not happen yet
                                  }
                              }
                          }
                      }
      
      
      so im looking for any idea how to force the JS.populateGrid() before the label texts are being printed...
      
      I tried even some ugly solution like this (which obviously did not help):
      

      Window {
      property variant gridValue: []
      property bool triggerPopulateGrid: false
      onTriggerPopulateGridChanged: { console.log("trying to trigger, but does not work, wont even compile (ninja error)"); JS.populateGrid(); }
      triggerPopulateGrid: true
      ....
      }

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @shokarta use your gridValue as a model of your Repeater:

      Repeater {
          model: gridValue
          delegate: Rectangle {            
              Label {
                  text: modelData.value
              }
          }
      }
      
      S 1 Reply Last reply
      0
      • GrecKoG GrecKo

        @shokarta use your gridValue as a model of your Repeater:

        Repeater {
            model: gridValue
            delegate: Rectangle {            
                Label {
                    text: modelData.value
                }
            }
        }
        
        S Offline
        S Offline
        shokarta
        wrote on last edited by shokarta
        #3

        @GrecKo sorry for misleading.

        the model in repeater is something totaly different then what i fill to gridValue[].
        This cant be used :(

        EDIT:
        thos i tried... it has a little relation,

        the way i do is that I have one repeater for model of 9,
        then inside that i have another repeater for another 9,
        therefore my delegated Rectangle is being shoed 91 times (9 times inside first repeater 9 times).
        And to my gridValue[] I do put 81 smaller arrays (so two-dim array).

        I tried to the second repeater put:

        model: gridValue.length / 9
        

        to see if i iwll force to load that Component.onCompleted from the top... but didnt solve anything... because at this time its not populated yed (therefore gridValue.lengthis 0, then second repeater wont do anything actually)

        so my problem persist even with you proposed solution

        J.HilkJ 1 Reply Last reply
        0
        • S shokarta

          @GrecKo sorry for misleading.

          the model in repeater is something totaly different then what i fill to gridValue[].
          This cant be used :(

          EDIT:
          thos i tried... it has a little relation,

          the way i do is that I have one repeater for model of 9,
          then inside that i have another repeater for another 9,
          therefore my delegated Rectangle is being shoed 91 times (9 times inside first repeater 9 times).
          And to my gridValue[] I do put 81 smaller arrays (so two-dim array).

          I tried to the second repeater put:

          model: gridValue.length / 9
          

          to see if i iwll force to load that Component.onCompleted from the top... but didnt solve anything... because at this time its not populated yed (therefore gridValue.lengthis 0, then second repeater wont do anything actually)

          so my problem persist even with you proposed solution

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @shokarta

          text: gridValue ? gridValue[index]['value'] : ""
          

          or better

          text: gridValue.length > index ? gridValue[index]['value'] : ""
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          S 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @shokarta

            text: gridValue ? gridValue[index]['value'] : ""
            

            or better

            text: gridValue.length > index ? gridValue[index]['value'] : ""
            
            S Offline
            S Offline
            shokarta
            wrote on last edited by
            #5

            @J-Hilk said in Windows {} COmponent.onCompleted is too late, how to trigger script as a first thing?:

            gridValue.length > index

            This also does not help...
            it leaves every label inside repeater as "",
            even when later the JS.populateGrid() is triggerd by Component.onCompleted, it wont change the labels, it does not update values from arrays :(

            so I realy need a method to force JS.populateGrid() before repeater is assembling it children

            J.HilkJ 1 Reply Last reply
            0
            • S shokarta

              @J-Hilk said in Windows {} COmponent.onCompleted is too late, how to trigger script as a first thing?:

              gridValue.length > index

              This also does not help...
              it leaves every label inside repeater as "",
              even when later the JS.populateGrid() is triggerd by Component.onCompleted, it wont change the labels, it does not update values from arrays :(

              so I realy need a method to force JS.populateGrid() before repeater is assembling it children

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @shokarta move your repeater in a loader and only activate that one after the js function is executed


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              S 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @shokarta move your repeater in a loader and only activate that one after the js function is executed

                S Offline
                S Offline
                shokarta
                wrote on last edited by
                #7

                @J-Hilk Yes!!!!
                great solution, works fine...
                thank you !!!!

                this brought one more additional issue which is probably trivial, but i dont know this :(

                so my scenario:

                Window {
                    property variant gridValue: []
                    Component.onCompleted: JS.populateGrid()
                    
                    
                    Rectangle {
                        
                        id: mainRect
                        anchors.fill: parent
                		
                        GridLayout {
                            
                            anchors.fill: parent
                                        
                            Loader {
                                id: gridLoader
                                anchors.fill: parent
                                sourceComponent: loaderComponent
                                active: false
                            }
                            
                            Component {
                                id: loaderComponent
                
                                Repeater {
                                                        
                                    model: 9
                                    
                					Rectangle {
                                        
                                        Layout.fillHeight: true
                                        Layout.fillWidth: true
                                        
                                        GridLayout {
                                            
                                            anchors.fill: parent
                                                                        
                                            Repeater {
                                                
                                                model: 9
                                                delegate: Rectangle {
                                                    
                                                    Layout.fillHeight: true
                                                    Layout.fillWidth: true
                                                    
                                                    Label {
                                                        anchors.centerIn: parent
                                                        text: gridValue.length > index ? gridValue[(rep2.rep2Index)*9+(index)]['value'] : ""
                                                    }
                                                    
                                                }
                                                
                                            }
                                        }
                                        
                                        
                                    }
                                    
                                }
                            }
                        }
                    }
                    
                }
                

                works now priperly just fine...
                inside the JS script i activate the loader once all script is done...

                however anchoring issue:

                QML Loader: Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead.
                

                on line of Loader {}

                can I kindly ask how this realy should be anchored?
                basicaly the Component should be anchored to parent GridLayout {}

                Thank you

                mzimmersM 1 Reply Last reply
                1
                • S shokarta

                  @J-Hilk Yes!!!!
                  great solution, works fine...
                  thank you !!!!

                  this brought one more additional issue which is probably trivial, but i dont know this :(

                  so my scenario:

                  Window {
                      property variant gridValue: []
                      Component.onCompleted: JS.populateGrid()
                      
                      
                      Rectangle {
                          
                          id: mainRect
                          anchors.fill: parent
                  		
                          GridLayout {
                              
                              anchors.fill: parent
                                          
                              Loader {
                                  id: gridLoader
                                  anchors.fill: parent
                                  sourceComponent: loaderComponent
                                  active: false
                              }
                              
                              Component {
                                  id: loaderComponent
                  
                                  Repeater {
                                                          
                                      model: 9
                                      
                  					Rectangle {
                                          
                                          Layout.fillHeight: true
                                          Layout.fillWidth: true
                                          
                                          GridLayout {
                                              
                                              anchors.fill: parent
                                                                          
                                              Repeater {
                                                  
                                                  model: 9
                                                  delegate: Rectangle {
                                                      
                                                      Layout.fillHeight: true
                                                      Layout.fillWidth: true
                                                      
                                                      Label {
                                                          anchors.centerIn: parent
                                                          text: gridValue.length > index ? gridValue[(rep2.rep2Index)*9+(index)]['value'] : ""
                                                      }
                                                      
                                                  }
                                                  
                                              }
                                          }
                                          
                                          
                                      }
                                      
                                  }
                              }
                          }
                      }
                      
                  }
                  

                  works now priperly just fine...
                  inside the JS script i activate the loader once all script is done...

                  however anchoring issue:

                  QML Loader: Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead.
                  

                  on line of Loader {}

                  can I kindly ask how this realy should be anchored?
                  basicaly the Component should be anchored to parent GridLayout {}

                  Thank you

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

                  @shokarta instead of anchors in your loader, use this:

                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  

                  Look at the QML Layout type for information on other options.

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

                    Use a proper TableView with a table model (TableModel or QAbstractTableModel)

                    1 Reply Last reply
                    1
                    • S shokarta has marked this topic as solved on

                    • Login

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