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. [SOLVED]How to Random the show of QML
Qt 6.11 is out! See what's new in the release blog

[SOLVED]How to Random the show of QML

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 17.2k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

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

    for example i have 5 QML

    qml1.qml
    qml2.qml
    qml3.qml
    qml4.qml
    qml5.qml

    and to view them i will use a loader element

    @
    Loader {
    id:mainLoader
    {
    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?

    1 Reply Last reply
    3
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Something to the effect of:

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

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      3
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0

          • Login

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