Important: Please read the Qt Code of Conduct -

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

  • 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

  • You should be able to get what you want by changing

    @var bg=[...]@


    @var bg=hex1[...]@


  • Moderators

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

  • 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

  • Moderators

    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.

  • 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 ^^

  • Moderators

    No problem! Glad to help!

Log in to reply