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]showing a Random words and sentences in a QML
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 3.1k Views
  • 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 Offline
    J Offline
    jr_jags
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      You should be able to get what you want by changing

      @var bg=[...]@

      to

      @var bg=hex1[...]@

      Regards,
      Michael

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

        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
        0
        • J Offline
          J Offline
          jr_jags
          wrote on last edited by
          #4

          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
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            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
            0
            • J Offline
              J Offline
              jr_jags
              wrote on last edited by
              #6

              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
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #7

                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
                0

                • Login

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