Results 1 to 7 of 7

Thread: how to implement QScriptValueIterator?

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to implement QScriptValueIterator?

    I don't understand how to implement QScriptValueIterator. I want to implement a script object that has some properties and also allows for java-style iteration in the script text.

    Documentation states:
    "If you want to iterate over the properties of a script object, use the QScriptValueIterator class."

    My code looks like this:
    Qt Code:
    1. static void registerWithScript(const char* scriptObjName, QScriptEngine& scriptengine)
    2. {
    3. T* t = new T();
    4. QScriptValue obj = scriptengine.newQObject(t, QScriptEngine::ScriptOwnership);
    5. scriptengine.setDefaultPrototype(qMetaTypeId<T>(),obj);
    6.  
    7. QScriptValue ctor = scriptengine.newFunction(objectConst);
    8. QScriptValue metaObject = scriptengine.newQMetaObject(&QObject::staticMetaObject, ctor);
    9. scriptengine.globalObject().setProperty(scriptObjName, metaObject);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Constructor of QScriptValueIterator requires QScriptValue object to be already created.

    So, if I modify my code to add

    Qt Code:
    1. QScriptValueIterator iter (obj);
    To copy to clipboard, switch view to plain text mode 
    after constructing the obj, the iterator seems to exist outside the scriptengine. Right?
    Who implements all these getNext, getPrev etc methods of QScriptValueIterator?

    Thanks.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to implement QScriptValueIterator?

    why do you want to implement it? It's already implemented...

    http://qt-project.org/doc/qt-4.8/qsc...eiterator.html
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to implement QScriptValueIterator?

    I don't undestand how QScriptValueIterator should be used. Exampe:

    I have class foo.

    Qt Code:
    1. class foo : public QObject
    2. {
    3. Q_PROPERTY(int size, READ getsize);
    4. ...
    5. private
    6. std::vector<int> _coll
    7. };
    To copy to clipboard, switch view to plain text mode 
    I can expose it to QtScript and then write a javascript function to be evaluated using foo instance
    Qt Code:
    1. function test()
    2. {
    3. var myfoo = new foo();
    4. var icount = myfoo.size;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Now I'd like to be able to iterate though the _coll member of foo. I can write custom methods and properties to do so, but there is also QScriptValueIterator in QtScript module. How (if at all) it should be used in this situation?

    I'll appreciate code example. Thanks.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to implement QScriptValueIterator?

    _coll isnt a property...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to implement QScriptValueIterator?

    amleto: I know that! ((c) Forrest Gump).

    I don't know how to make my question any clearer, but let me try again.

    Using the class foo from my previous message: how to expose _coll member of class foo to the scripting environment using QScriptValueIterator?

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to implement QScriptValueIterator?

    err... make it a property
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to implement QScriptValueIterator?

    QScriptValueIterator is a helper for the C++ side, i.e. to iterate over an QScriptValue instance's properties.

    It is not for implementing iterators on the script side.

    What you might want to use is qScriptRegisterSequenceMetaType and exposing your container as a propery
    http://qt-project.org/doc/qt-4.8/qsc...quenceMetaType

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    TorAn (4th December 2012)

Similar Threads

  1. what is the best way to implement ?
    By david_itay in forum Newbie
    Replies: 1
    Last Post: 8th August 2012, 23:10
  2. Trying to implement QMainWindow...
    By winston2020 in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2008, 20:12
  3. how to implement this widget
    By pencilren in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2007, 12:10
  4. What's the best way to implement a MainWindow
    By darkadept in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2007, 18:46
  5. how to implement it ?
    By qtopiahooo in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2006, 18:01

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.