Results 1 to 2 of 2

Thread: Accessing QtScript arrays from C++

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Accessing QtScript arrays from C++

    I wondering how to access an (QScript) array from C++.

    We can use QScriptValue::isArray() to determine if the value is an array or not. Unfortunately, it doesn’t really

    state there how to access contents of the array.My initial reaction was to just convert the value to a

    QVariant (using toVariant()) and then convert the variant to a QVariantList (toList()). That didn’t quite work.

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Accessing QtScript arrays from C++

    Works for me. Can you post your code?
    Qt Code:
    1. #include <QtGui>
    2. #include <QtScript>
    3.  
    4. int main(int argc, char * argv[])
    5. {
    6. QApplication a(argc, argv);
    7. QScriptEngine eng;
    8. QScriptValue val = eng.evaluate("[1, 2, 3, 'four', 'five']");
    9.  
    10. qDebug() << val.isArray();
    11.  
    12. QVariantList arr = val.toVariant().toList();
    13.  
    14. foreach (QVariant item, arr)
    15. {
    16. qDebug() << item;
    17. }
    18.  
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to numbat for this useful post:

    wookoon (8th June 2010)

Similar Threads

  1. Replies: 0
    Last Post: 25th November 2009, 07:46
  2. debugging arrays
    By divide in forum Qt Tools
    Replies: 1
    Last Post: 12th October 2009, 21:17
  3. Accessing array in both C++ and QtScript
    By abernat in forum Qt Programming
    Replies: 10
    Last Post: 6th September 2009, 22:21
  4. QtScript and arrays
    By supergillis in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2009, 19:13
  5. creating 2-d arrays
    By therealjag in forum Newbie
    Replies: 1
    Last Post: 21st February 2006, 17:41

Tags for this Thread

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.