Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED]showing a Random words and sentences in a QML

    QML and Qt Quick
    3
    7
    2590
    Loading More Posts
    • 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.
    • J
      jr_jags last edited by

      i want to show random words and sentences in my QML,
      @import QtQuick 1.0
      import "random.js" as Random

      Rectangle {
      width: 360
      height: 360

      Text {
          id: display
          x: 76
          y: 110
          text: Random.randomBg();
          anchors.verticalCenterOffset: -63
          anchors.horizontalCenterOffset: -77
          wrapMode: Text.WrapAtWordBoundaryOrAnywhere
          anchors.centerIn: parent
      }
      
      MouseArea {
          id: mouse_area1
          x: 222
          y: 297
          width: 100
          height: 42
          drag.minimumY: -1000
          drag.minimumX: -1000
          drag.maximumY: 1000
          drag.maximumX: 1000
          onClicked: {
      
      
          }
      }
      

      }
      @

      this is my QML and i want to show my random words to my text element but it says Unable to assign QVariantList to QString, how can i show them??

      @
      //Here is my js file
      function randomBg()

      {
      var hex1=new Array('We Stand Alone Together!!', 'Ayaw', 'Gumana', 'Weaklings!')
      var hex2=new Array("Currahee!!")
      var hex3=new Array("Band of Brothers")
      var hex4=new Array("As I Lay Dying")
      var hex5=new Array("Heroes are all Brave")
      var hex6=new Array("Astig!")
      var bg=[Math.floor(hex1.length*Math.random())]
      return bg
      }
      @

      1 Reply Last reply Reply Quote 0
      • M
        mbrasser last edited by

        You should be able to get what you want by changing

        @var bg=[...]@

        to

        @var bg=hex1[...]@

        Regards,
        Michael

        1 Reply Last reply Reply Quote 0
        • M
          mlong last edited by

          Although, as best as I can see, hex2 through hex6 seem to be ignored altogether.

          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 Reply Quote 0
          • J
            jr_jags last edited by

            i changed my js file and works but it only shows the last word in my hex1, it doesnt change

            @function randomBg()

            {
            var hex1=new Array("We Stand Alone Together!!", "Ayaw", "Gumana", "Weaklings!")

            var bg=hex1[Math.floor(hex1.length*Math.random())]
            return bg
            }
            @

            1 Reply Last reply Reply Quote 0
            • M
              mlong last edited by

              I tried the code you posted and I'm getting a different random value every time I call randomBg().

              Looks like it should be working.

              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 Reply Quote 0
              • J
                jr_jags last edited by

                oh, thanks, sorry for that, everytime i run the program it only shows the same value, but when o include tha randomBg() in different text element it shows random words fromthe list,
                Thanks ^^

                1 Reply Last reply Reply Quote 0
                • M
                  mlong last edited by

                  No problem! Glad to help!

                  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 Reply Quote 0
                  • First post
                    Last post