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. Error: Qt.createQmlObject(): failed to create object: Non-existent attached object
QtWS25 Last Chance

Error: Qt.createQmlObject(): failed to create object: Non-existent attached object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
createobjectgridlayoutjavascriptcreateqmlobject
13 Posts 4 Posters 2.9k 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.
  • K Offline
    K Offline
    Kyeiv
    wrote on last edited by Kyeiv
    #1

    I have a QVariantMap which i use to create children of GridLayout

    my code:

    import QtUtils 1.0
    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.2
    Item
    {
        Connections
        {
            target: model
            onModelChanged: updateSummaryList()
        }
        function updateSummaryList() {
            for(var i = summaryGrid.children.length; i > 0 ; i--) {
                console.log("destroying: " + i)
                summaryGrid.children[i-1].destroy()
            }
    
            try
            {
                for (var prop in model.map) {
                    var labelText = Qt.createQmlObject('import QtQuick 2.5;Text { text: "first text:"; color: "white"; horizontalAlignment: Text.AlignRight; Layout.alignment: Qt.AlignRight; }',
                        summaryGrid,
                        prop+"label");
                    
                    var valueText = Qt.createQmlObject('
                        import QtQuick 2.5;\
                        Text { text: "text"; color: "white";}',
                        summaryGrid,
                        prop+"value");
                }
    
            }
            catch (error)
            {
                console.error(error);
            }
        }
                    GridLayout {
                                id: summaryGrid
                                Component.onCompleted: updateSummaryList()
                                columns: 2
                            }
    }
    

    When i run this code i get:

    Error: Qt.createQmlObject(): failed to create object: field1label:1:102: Non-existent attached object
    

    What may be the reason?

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #13

      A ListView appears to be a better match than the more complicated GridLayout and Repeater.

      ListView {
          delegate: Item {
              width: ListView.view.width
              height: childrenRect.height
              Text {
                  anchors.right: parent.horizontalCenter
                  text: modelData.firstPart + ":"
              }
              Text {
                  anchors.left: parent.horizontalCenter
                  text: modelData.secondPart
              }
          }
      }
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        The only attached object is Layout so it's probably the reason to fail: QML engine does not see the GridLayout when creating your object.

        Rather than fixing this, though, I'd recommend using a completely different approach:

        • use Repeater in your GridLayout to create the items you need
        • pass the model you have to Reapeater so that it updates automatically (there will be no more any need for Connections component)
        • do not create components in QML dynamically

        This should greatly simplify the logic.

        (Z(:^

        K 1 Reply Last reply
        3
        • sierdzioS sierdzio

          The only attached object is Layout so it's probably the reason to fail: QML engine does not see the GridLayout when creating your object.

          Rather than fixing this, though, I'd recommend using a completely different approach:

          • use Repeater in your GridLayout to create the items you need
          • pass the model you have to Reapeater so that it updates automatically (there will be no more any need for Connections component)
          • do not create components in QML dynamically

          This should greatly simplify the logic.

          K Offline
          K Offline
          Kyeiv
          wrote on last edited by Kyeiv
          #3

          @sierdzio said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

          • use Repeater in your GridLayout to create the items you need

          But Repeater supports only one Component as delegate. I want it to fill 2 columns of GridLayout with 2 Text delegates.

          Static grid that i am trying to make dynamic:

          GridLayout {
                                      id: grid
                                      columns: 2
          
                                      Text { 
                                          text: "first text:"; 
                                          color: "white"; 
                                          horizontalAlignment: Text.AlignRight
                                          Layout.alignment: Qt.AlignRight
                                          }
                                      Text { text: "text"; color: "white"}
                                      Text { 
                                          text: "second text:"; 
                                          color: "white"; 
                                          horizontalAlignment: Text.AlignRight
                                          Layout.alignment: Qt.AlignRight
                                          }
                                      Text { text: "text text"; color: "white"}
                                      Text { 
                                          text: "the third text:"; 
                                          color: "white"; 
                                          horizontalAlignment: Text.AlignRight
                                          Layout.alignment: Qt.AlignRight
                                          }
                                      Text { text: "text text text"; color: "white"}
                                  }
          

          Its output:

                                  first text: text
                                 second text: text text
                              the third text: text text text
          
          jeremy_kJ 1 Reply Last reply
          0
          • K Kyeiv

            @sierdzio said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

            • use Repeater in your GridLayout to create the items you need

            But Repeater supports only one Component as delegate. I want it to fill 2 columns of GridLayout with 2 Text delegates.

            Static grid that i am trying to make dynamic:

            GridLayout {
                                        id: grid
                                        columns: 2
            
                                        Text { 
                                            text: "first text:"; 
                                            color: "white"; 
                                            horizontalAlignment: Text.AlignRight
                                            Layout.alignment: Qt.AlignRight
                                            }
                                        Text { text: "text"; color: "white"}
                                        Text { 
                                            text: "second text:"; 
                                            color: "white"; 
                                            horizontalAlignment: Text.AlignRight
                                            Layout.alignment: Qt.AlignRight
                                            }
                                        Text { text: "text text"; color: "white"}
                                        Text { 
                                            text: "the third text:"; 
                                            color: "white"; 
                                            horizontalAlignment: Text.AlignRight
                                            Layout.alignment: Qt.AlignRight
                                            }
                                        Text { text: "text text text"; color: "white"}
                                    }
            

            Its output:

                                    first text: text
                                   second text: text text
                                the third text: text text text
            
            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @Kyeiv said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

            @sierdzio said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

            • use Repeater in your GridLayout to create the items you need

            But Repeater supports only one Component as delegate. I want it to fill 2 columns of GridLayout with 2 Text delegates.

            Wrap the 2 delegates in a single component and use the index property to decide which to use for each cell.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            K 1 Reply Last reply
            0
            • jeremy_kJ jeremy_k

              @Kyeiv said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

              @sierdzio said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

              • use Repeater in your GridLayout to create the items you need

              But Repeater supports only one Component as delegate. I want it to fill 2 columns of GridLayout with 2 Text delegates.

              Wrap the 2 delegates in a single component and use the index property to decide which to use for each cell.

              K Offline
              K Offline
              Kyeiv
              wrote on last edited by
              #5

              @jeremy_k I am not sure how to achieve it, can you provide some sample code?

              1 Reply Last reply
              0
              • jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #6
                GridLayout {
                    columns: 2
                    Repeater {
                        model: 10
                        delegate: Text {
                                text: index % 2 == 0 ? "text " + index : "text" * index
                        }
                    }
                }
                

                For more complicated or external delegates, substitute DelegateChooser or Loader for the Text item.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                K 1 Reply Last reply
                0
                • jeremy_kJ jeremy_k
                  GridLayout {
                      columns: 2
                      Repeater {
                          model: 10
                          delegate: Text {
                                  text: index % 2 == 0 ? "text " + index : "text" * index
                          }
                      }
                  }
                  

                  For more complicated or external delegates, substitute DelegateChooser or Loader for the Text item.

                  K Offline
                  K Offline
                  Kyeiv
                  wrote on last edited by
                  #7

                  @jeremy_k But my model is in format of QVariantMap, for example:

                  "key" : { "label": "first text"; "value": "text" }
                  "key2" : { "label": "second text"; "value": "text text" }
                  

                  so i cannot see how to take advantage of indexes in here.

                  jeremy_kJ 1 Reply Last reply
                  0
                  • K Kyeiv

                    @jeremy_k But my model is in format of QVariantMap, for example:

                    "key" : { "label": "first text"; "value": "text" }
                    "key2" : { "label": "second text"; "value": "text text" }
                    

                    so i cannot see how to take advantage of indexes in here.

                    jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote on last edited by jeremy_k
                    #8

                    @Kyeiv Use a model.

                    Edit: QVariantMap might be directly usable as a model for a repeater as well. If so, index is available, as is modelData.

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    K 1 Reply Last reply
                    0
                    • jeremy_kJ jeremy_k

                      @Kyeiv Use a model.

                      Edit: QVariantMap might be directly usable as a model for a repeater as well. If so, index is available, as is modelData.

                      K Offline
                      K Offline
                      Kyeiv
                      wrote on last edited by Kyeiv
                      #9

                      @jeremy_k yes but still if so, i cannot take advantage of the index property when i use one object, let's say of index 1 to fill 2 Texts

                      i need something like:

                      GridLayout {
                          columns: 2
                          Repeater {
                              model: model.map
                              delegate: Text {
                                      text: modelData.label
                                      horizontalAlignment: Text.AlignRight
                                      Layout.alignment: Qt.AlignRight
                              }
                             Text {
                                      text: modelData.value
                              }
                          }
                      }
                      
                      jeremy_kJ fcarneyF 2 Replies Last reply
                      0
                      • K Kyeiv

                        @jeremy_k yes but still if so, i cannot take advantage of the index property when i use one object, let's say of index 1 to fill 2 Texts

                        i need something like:

                        GridLayout {
                            columns: 2
                            Repeater {
                                model: model.map
                                delegate: Text {
                                        text: modelData.label
                                        horizontalAlignment: Text.AlignRight
                                        Layout.alignment: Qt.AlignRight
                                }
                               Text {
                                        text: modelData.value
                                }
                            }
                        }
                        
                        jeremy_kJ Offline
                        jeremy_kJ Offline
                        jeremy_k
                        wrote on last edited by
                        #10

                        @Kyeiv You might be better off starting from a sketch or a description the use case. It looks like this is implementing the hard way, perhaps porting from a toolkit with a different structure.

                        Asking a question about code? http://eel.is/iso-c++/testcase/

                        K 1 Reply Last reply
                        0
                        • jeremy_kJ jeremy_k

                          @Kyeiv You might be better off starting from a sketch or a description the use case. It looks like this is implementing the hard way, perhaps porting from a toolkit with a different structure.

                          K Offline
                          K Offline
                          Kyeiv
                          wrote on last edited by
                          #11

                          @jeremy_k
                          what i have: a map of objects which have 2 attributes - label and value.
                          what i want to achieve: the formatted list of them in form of:

                                                  first label text: value text
                                                 second label text: value text
                                              the third label text: its value text
                          
                          

                          so its centered, the " : " in the middle.

                          1 Reply Last reply
                          0
                          • K Kyeiv

                            @jeremy_k yes but still if so, i cannot take advantage of the index property when i use one object, let's say of index 1 to fill 2 Texts

                            i need something like:

                            GridLayout {
                                columns: 2
                                Repeater {
                                    model: model.map
                                    delegate: Text {
                                            text: modelData.label
                                            horizontalAlignment: Text.AlignRight
                                            Layout.alignment: Qt.AlignRight
                                    }
                                   Text {
                                            text: modelData.value
                                    }
                                }
                            }
                            
                            fcarneyF Offline
                            fcarneyF Offline
                            fcarney
                            wrote on last edited by
                            #12

                            @Kyeiv said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:

                            GridLayout {
                            columns: 2
                            Repeater {
                            model: model.map
                            delegate: Text {
                            text: modelData.label
                            horizontalAlignment: Text.AlignRight
                            Layout.alignment: Qt.AlignRight
                            }
                            Text {
                            text: modelData.value
                            }
                            }
                            }

                            Then just put them in one delegate item:

                            GridLayout {
                                columns: 2
                                Repeater {
                                    model: model.map
                                    delegate: Column {
                                    Text {
                                            text: modelData.label
                                            horizontalAlignment: Text.AlignRight
                                            Layout.alignment: Qt.AlignRight
                                    }
                                   Text {
                                            text: modelData.value
                                    }
                                    }
                                }
                            }
                            

                            C++ is a perfectly valid school of magic.

                            1 Reply Last reply
                            0
                            • jeremy_kJ Offline
                              jeremy_kJ Offline
                              jeremy_k
                              wrote on last edited by
                              #13

                              A ListView appears to be a better match than the more complicated GridLayout and Repeater.

                              ListView {
                                  delegate: Item {
                                      width: ListView.view.width
                                      height: childrenRect.height
                                      Text {
                                          anchors.right: parent.horizontalCenter
                                          text: modelData.firstPart + ":"
                                      }
                                      Text {
                                          anchors.left: parent.horizontalCenter
                                          text: modelData.secondPart
                                      }
                                  }
                              }
                              

                              Asking a question about code? http://eel.is/iso-c++/testcase/

                              1 Reply Last reply
                              1

                              • Login

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