Nominate our 2022 Qt Champions!

[SOLVED]How to Random the show of QML

  • I want to Random the showof QML when a mousearea is clicked

    for example i have 5 QML


    and to view them i will use a loader element

    Loader {
    MouseArea {
    id: mouse_area4
    x: 35
    y: 243
    width: 100
    height: 17
    onClicked: mainLoader.source ="";//the source will choose from the 5 qml

    Im thinking of using js is it possibleto use qml's in an array and Randomized it for viewing?
    How can i do this?

  • Something to the effect of:

    onClicked: mainloader.source = "qml" + Math.ceil(Math.random() * 5) + ".qml"

  • what if i have other qml's and i only want to random is the 5 qml's which is qml1 - qml5

    ive created a js file
    @function randomqml ()
    var qml= new Array ("qml1.qml", "qml2.qml", "qml3.qml", "qml4.qml", "qml5.qml")

    var bg
    var bg = qml[Math.floor(qml.length*Math.random)]
    return bg


    @onClicked: mainLoader.source = Random.randomqml();

    and when i click the mousearea it return an error of Cannot assign [undefined] to QUrl

  • oh now i got it! Thanks for your code ^^

    @onClicked: mainloader.source = "qml" + Math.ceil(Math.random() * 5) + ".qml"@

    i do not need an array script for this one

Log in to reply