Results 1 to 2 of 2

Thread: QtScript and arrays

  1. #1
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QtScript and arrays

    I can make an array in QEngine and evaluate it, but I don't have any idea of how to get each element of the array to a QStringList or a normal QList...
    This is probably a stupid question since I can't find anything on this forum about it . But I'm asking it anyway because I really want to know it!

    Here's what I already got:
    Qt Code:
    1. QScriptValue array = engine.evaluate("new Array(\"a\", \"b\", \"c\")");
    2. QVariant var = array.toVariant();
    3. if(var.canConvert<QStringList>())
    4. {
    5. QStringList list = var.value<QStringList>();
    6. qDebug() << "can convert";
    7. qDebug() << list.count();
    8. while(list.count())
    9. qDebug() << list.takeAt(0);
    10. }
    To copy to clipboard, switch view to plain text mode 
    And this is the output:
    can convert
    1
    "a,b,c"
    As you can see, the QStringList only contains one element: "a,b,c"...

    Thanks,
    Gillis

  2. #2
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtScript and arrays

    Nevermind, I already found out.
    Qt Code:
    1. QScriptValue array = engine.evaluate("new Array(\"a\", \"b\", \"c\")");
    2. int length = array.property("length").toInteger();
    3. for(int i = 0; i < length; i++)
    4. qDebug() << array.property(i).toString();
    To copy to clipboard, switch view to plain text mode 

    Thanks anyway
    Gillis

  3. The following 3 users say thank you to supergillis for this useful post:

    bunjee (20th May 2009), Lis (27th August 2010), tdewolf (25th October 2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.