Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML (JavaScript) how to create dynamic object?



  • The createRoom function does not work.
    What I want is to create a Dynamic Room object inside the RoomPage.

    How can I solve this problem?

    RoomSelect.qml

    import QtQuick 2.4
    
    RoomSelectForm {
        function createRoom(id,name,type,price,visible){
            var component;
            var object;
            component = Qt.createComponent("Room.qml")
            object  = component.createObject(roomPage2.roomLayout,{"x":0 , "y" : 0});
            object.roomId = id;
            object.roomName = name;
            object.roomType = type;
            object.roomPrice = price;
            object.roomVisible = visible;
            return object;
        }
    }
    

    RoomSelectForm.ui.qml

    import QtQuick 2.4
    import QtQuick.Controls 2.3
    import QtQuick.Layouts 1.3
    
    Item {
        id: item1
        property alias roomPage2: roomPage2
    
    // blablablabla...
    
            SwipeView {
                id: swipeView
    
                RoomPage{
                    id:roomPage1
                }
                RoomPage{
                    id:roomPage2
                }
                RoomPage{
                    id:roomPage3
                }
        }
    }
    
    

    RoomPage.qml

    import QtQuick 2.0
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.2
    
    Item {
        property alias roomLayout: roomLayout
    
    // blablablablabla...
    
            GridLayout {
                id: roomLayout
            }
    }
    

    Room.qml

    import QtQuick 2.4
    
    Item {
        property int roomId: -1
        property alias roomName: roomName.text
        property alias roomType: roomType.text
        property alias roomPrice: roomPrice.text
        property alias roomVisible: room.visible
    
    Rectangle{
    // blablablablabla...
    }
    
    }
    

    please help me....


  • Qt Champions 2017

    Does not work means ? What is the behavior ? You are creating local variable. Did you observe that ?



  • @dheerendra

    Sorry.
    Some content is missing.

    I want to call the createRoom function in C ++.

    main.cpp

        QQmlEngine *engine = ui->quickWidget->engine();
    
        QQmlComponent qml_RoomSelectCom(engine,QUrl("qrc:/RoomSelect.qml"));
    
        QObject *qml_RoomSelectObj = qml_RoomSelectCom.create();
    
        QVariant qml_rooms;
    
    QMetaObject::invokeMethod(qml_RoomSelectObj,"createRoom",Q_RETURN_ARG(QVariant,qml_rooms),Q_ARG(QVariant,5),Q_ARG(QVariant,"Test"),Q_ARG(QVariant,"wqe"),Q_ARG(QVariant,"1230"),Q_ARG(QVariant,true));
       
    

    The createRoom function is executed but the Room object does not appear on the screen.
    I think the use of this invokeMethod function seems to be wrong.

    How can I solve it?


Log in to reply