Results 1 to 5 of 5

Thread: QtScript: Properties of the object inherited from QObjectList are not visible from JS

  1. #1
    Join Date
    Sep 2012
    Location
    Kharkiv
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QtScript: Properties of the object inherited from QObjectList are not visible from JS

    I'm trying to expose to JS the audioTracks object which should be accessible from JS as array (audioTracks[index]) and at the same time should have method audioTracks.getTrackById("id").
    Here is my object:
    Qt Code:
    1. class AudioTrackList : public QObject, public QObjectList
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AudioTrackList(ScriptableIfaceMedia*);
    7.  
    8. Q_PROPERTY(int length READ size SCRIPTABLE true)
    9.  
    10. Q_INVOKABLE QObject* getTrackById(QString id);
    11.  
    12. private:
    13. ScriptableIfaceMedia* m_impl;
    14. QScriptEngine m_engine;
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 
    The problem is that when I expose the object in the following way:
    Qt Code:
    1. Q_PROPERTY(QObjectList audioTracks READ audioTracks SCRIPTABLE true)
    2. QObjectList audioTracks();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QObjectList MediaTracks::audioTracks()
    2. {
    3. int i;
    4.  
    5. QScriptValue rv = m_engine.newArray(m_audio_tracks->size());
    6. for (i = 0; i < m_audio_tracks->size(); i++) {
    7. rv.setProperty(i, m_engine.newQObject(m_audio_tracks->at(i), QScriptEngine::QtOwnership));
    8. }
    9.  
    10. return static_cast<QObjectList>(*m_audio_tracks);
    11. }
    To copy to clipboard, switch view to plain text mode 
    I have audioTracks[index] working but audioTracks.getTrackById("id") not.
    Instead when I expose the object another way:
    Qt Code:
    1. Q_PROPERTY(QObject* audioTracks READ audioTracks SCRIPTABLE true)
    2. QObject* audioTracks();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QObject* MediaTracks::audioTracks()
    2. {
    3. m_engine.newQObject(m_audio_tracks, QScriptEngine::QtOwnership);
    4. return m_audio_tracks;
    5. }
    To copy to clipboard, switch view to plain text mode 
    I have audioTracks.getTrackById("id") working but audioTracks[index] not.
    Is there any way to export my object to make it possible to access it from JS both like array and like object?

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript: Properties of the object inherited from QObjectList are not visible fro

    After having introduced the qobject into script, get hold of the array QScriptValue. The idea is to introduce a native function to the the qobject.
    Qt Code:
    1. QScriptValue arrayVal = objectScriptVal.property(QString("propertyName"));
    2. ...
    3. QScriptValue someFunctionName(QScriptContext *ctx, QScriptEngine *eng)
    4. {
    5. QObject* obj = ctx->thisObject()->toQObject();
    6. if (obj){
    7. MyClass* myClassObj = qobject_cast<MyClass*>(obj);
    8. QObject* result = myClassObj->function(ctx->argument(0).toString());
    9. return result; // or return eng->newQObject(result); //both should work
    10. }
    11. ...
    12. arrayVal.setProperty("functionName", engine->newFunction(someFunctionName));
    To copy to clipboard, switch view to plain text mode 
    In jist: here what has been done.
    1. Introduced object with an array property.
    2. Got hold of QScriptValue of array property.
    3. Introduce a native function to the array property.

  3. #3
    Join Date
    Sep 2012
    Location
    Kharkiv
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QtScript: Properties of the object inherited from QObjectList are not visible fro

    Still can't make it to work
    pkj, did you manage to do such trick?
    It seems that when my AudioTrackList is exposed to JS like array (by newArray()) then only standard JS array methods and properties are accessible.
    When I try to call getTrackById() method I get such error:
    http://192.168.23.2/play.html:94: TypeError: 'undefined' is not a function (evaluating 'p.audioTracks.getTrackById("id1")')

  4. #4
    Join Date
    Sep 2012
    Location
    Kharkiv
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QtScript: Properties of the object inherited from QObjectList are not visible fro

    I just found out that when I expose my object by QScriptEngine::newArray() I can't add to my object even a simple property like:
    Qt Code:
    1. Q_PROPERTY(int myprop READ myprop SCRIPTABLE true)
    2. int myprop() const;
    To copy to clipboard, switch view to plain text mode 
    In JavaScript it becomes a usual array.

  5. #5
    Join Date
    Sep 2012
    Location
    Kharkiv
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QtScript: Properties of the object inherited from QObjectList are not visible fro

    I have found a solution for my problem.
    Since in JavaScript .0 and ['0'] are the same things I made the following workaround in my code:
    Qt Code:
    1. value = m_engine.newQObject(m_audio_tracks, QScriptEngine::QtOwnership);
    2. for (i = 0; i < m_audio_tracks->tracks().size(); i++) {
    3. m_audio_tracks->setProperty(QString("%1").arg(i).toLocal8Bit().data(), qVariantFromValue(m_audio_tracks->tracks().at(i)));
    4. value.setProperty(QString("%1").arg(i), m_engine.newQObject(m_audio_tracks->tracks().at(i), QScriptEngine::QtOwnership));
    5. }
    To copy to clipboard, switch view to plain text mode 
    to make it possible to access my object like array from JavaScript.

Similar Threads

  1. Replies: 0
    Last Post: 7th July 2011, 12:06
  2. Calling object's operator+ from QtScript
    By PolyVox in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2010, 22:37
  3. Inherited Properties on Custom Widgets
    By mbreier in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2010, 07:30
  4. Mapping JS Object properties to QProperties
    By Scorp2us in forum Qt Programming
    Replies: 0
    Last Post: 29th June 2009, 20:52
  5. [QtScript] One getter/setter function for multiple properties
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2009, 09:52

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
  •  
Qt is a trademark of The Qt Company.