1

I have two questions.I am creating an app with Qt. I am using qml and js for my frontend code. I am trying to find a way to create multiple objects dynamically inside a ColumnLayout{}. Lets assume those objects will be rectangles.The server will send me an array with data (images, texts and types) I will calculate array's length and i will add those data to each rectangle. How will i be able to add them into the rectangles? I will try to make the code simple and cut all the unnecessary parts. What you will see it is probably wrong but this was the only thing that came in my mind.

Item {
    id:main_container
    width: parent.width
    height: parent.height

    //////////////////////js///////////////////////////////////////

    function test(){
            var foo = new Array(2);
            for(var i = 0; i < foo.length; i++){
                console.log("i am here");
                var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle {  color: "red"; border.color: "black"; border.width: 5; width: 150; height: 150}',body);//i noticed Layout.preferedWidth & height here occurs error so i have to use width and height
            }
        }
    ////////////////////////////////
     Rectangle{
        id: home_main_container
        anchors.fill: parent
        Rectangle{
            id:header
            width: parent.width
            height: parent.height/10
            anchors.top: parent.top
            color: b1
        }
        Rectangle{
            id:body
            width: parent.width
            height: parent.height*(8/10)
            anchors.top: header.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            ColumnLayout{
                id:body_part1
                width: parent.width
                height: parent.height/4
                anchors.top: parent.bottom
                Component.onCompleted: test();

            }
        }
        Rectangle{
            id:footer
            width: parent.width
            height: parent.height/10
            anchors.bottom: parent.bottom
            color: b1
        }
    }
}


2
  • Did you try this code? What specific problem are you having with it? Commented Sep 9, 2020 at 17:54
  • Height and width i want to be Layout.preferredHeight: parent.height/2 and Layout.preferredWidth: parent.width. But when i am inserting them like this i am getting error. Also i don't know how to insert in Qt.createQmlObject('...') a text from the array of data. example, if i want the second rectangle to have the 2nd value of the array as text inside of it. Commented Sep 9, 2020 at 19:03

2 Answers 2

2

You can use the model paradigm that qml offers. Here is the Documentation. https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for contributing. Your post is really more of a comment than an answer though. If you are suggesting a better way for the OP to do something, show him how to do it rather than just pointing at documentation.
0

To use Layouts within your dynamically created object, you just need to include the import statement.

var newObject = Qt.createQmlObject('import QtQuick 2.12; import QtQuick.Layouts 1.12; Rectangle { color: "red"; border.color: "black"; border.width: 5; Layout.preferredWidth: parent.width; Layout.preferredHeight: parent.height / 2; }', body_part1);

When I tried this, I noticed something weird in your ColumnLayout code that was tripping me up.

ColumnLayout{
    id:body_part1
    width: parent.width
    height: parent.height/4
    //anchors.top: parent.bottom   // I had to comment this out or the items were outside the visible area.
    Component.onCompleted: test();
}

Your second question appears to be just about adding expressions to text strings. You can do this easily with the + operator:

var myString = "Hello " + getWorld();

This will work in the createQmlObject function just fine:

Qt.createQmlObject("import QtQuick... someValue: " + value1 + "; someOtherValue: " + value2) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.