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

Can't a QML file contain more than one QML components?



  • As title, I used qmlscene to interpret a QML file from a QML book, "Qt5 Cadaques", as below. The qmlscene shows that the error at Line of Text component. I tried some other cases, the same thing. How come? The examples come from the Qt book.

    Rectangle { // our inlined button ui
    id: button
    x: 12; y: 12
    width: 116; height: 26
    color: "lightsteelblue"
    border.color: "slategrey"
    Text {
    anchors.centerIn: parent
    text: "Start"
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    status.text = "Button clicked!"
    }
    }
    }
    Text { // text changes when button was clicked
    id: status
    x: 12; y: 76
    width: 116; height: 26
    text: "waiting ..."
    horizontalAlignment: Text.AlignHCenter
    }


  • Moderators

    Hi @Stan-Huang
    due to how QMl is implemented behind the scenes, a QMl-File must always have 1 single root element.

    your Text Item is a sibling to the Rectangle -> that is not possible


    https://doc.qt.io/qt-5/qtqml-documents-structure.html



  • @J.Hilk I added another component to contain the two and found it works. Thanks.


Log in to reply