Results 1 to 4 of 4

Thread: How to register pointer type in qScriptRegisterMetatype?

  1. #1
    Join Date
    Aug 2012
    Location
    USSR
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to register pointer type in qScriptRegisterMetatype?

    Hello.

    How to register pointer type in qScriptRegisterMetatype?

    h-file.

    Qt Code:
    1. namespace PFT3 {
    2.  
    3. class TaskEngine : public QObject {
    4. Q_OBJECT
    5. public:
    6. static const char * TaskEngine::nameInScript;
    7. static const char * TaskEngine::scriptTasksNamespaceName;
    8. void createInQScriptEngine(QScriptEngine &scriptEngine);
    9. };
    10.  
    11. namespace PFT3_Tasks {
    12.  
    13. class LIBPFT3SHARED_EXPORT BaseData : public QObject {
    14. Q_OBJECT
    15. };
    16.  
    17. class BaseDataScriptProto : public QObject, protected QScriptable {
    18. Q_OBJECT
    19. };
    20.  
    21. }// end of namespace PFT3_Tasks
    22.  
    23. }//end of PFT3 namespace
    24.  
    25. Q_DECLARE_METATYPE(PFT3::PFT3_Tasks::BaseData)
    26. Q_DECLARE_METATYPE(PFT3::PFT3_Tasks::BaseData*)
    27. Q_DECLARE_METATYPE(const PFT3::PFT3_Tasks::BaseData*)
    To copy to clipboard, switch view to plain text mode 

    cpp-file

    Qt Code:
    1. QScriptValue PFT3TaskBaseDataToScriptValue(QScriptEngine *engine, PFT3::PFT3_Tasks::BaseData * const &in){
    2. return engine->newQObject(in, QScriptEngine::AutoOwnership);
    3. }
    4.  
    5. void PFT3TaskBaseDataFromScriptValue(const QScriptValue &object, PFT3::PFT3_Tasks::BaseData * &out){
    6. out = qobject_cast<PFT3::PFT3_Tasks::BaseData*>(object.toQObject());
    7. }
    8.  
    9. namespace PFT3 {
    10.  
    11. const char * TaskEngine::nameInScript = "pft3TaskEngine";
    12. const char * TaskEngine::scriptTasksNamespaceName = "PFT3_Tasks";//Changing this name doesn't changes 'PFT3_Tasks::BaseData*' string in error. This is only coincidence.
    13.  
    14. void TaskEngine::createInQScriptEngine(QScriptEngine &scriptEngine){
    15. /*Create single for given scriptEngine TaskEngine*/
    16. QScriptValue scriptTaskEngineObject = scriptEngine.newQObject(this);
    17. scriptEngine.globalObject().setProperty(nameInScript, scriptTaskEngineObject);
    18. /*Adding prototypes*/
    19. QScriptValue scriptTasksNamespace = scriptEngine.newQObject(new QObject(), QScriptEngine::ScriptOwnership);
    20. scriptEngine.globalObject().setProperty(scriptTasksNamespaceName, scriptTasksNamespace);
    21. /*BaseData*/
    22. PFT3_Tasks::BaseDataScriptProto *baseDataPrototypeObject = new PFT3_Tasks::BaseDataScriptProto (&scriptEngine);
    23. const QScriptValue baseDataProto = scriptEngine.newQObject(baseDataPrototypeObject, QScriptEngine::ScriptOwnership);
    24. scriptEngine.setDefaultPrototype(qMetaTypeId<PFT3::PFT3_Tasks::BaseData>(), baseDataProto);
    25. QScriptValue baseDataCtor = scriptEngine.newFunction(&PFT3_Tasks::BaseData::construct, baseDataProto);
    26. scriptEngine.globalObject().property(scriptTasksNamespaceName).setProperty("BaseData", baseDataCtor);
    27.  
    28. /*-------------------------------------------------------------------------------------------------------*/
    29. qScriptRegisterMetaType(&scriptEngine, PFT3TaskBaseDataToScriptValue, PFT3TaskBaseDataFromScriptValue);//I register it here.
    30. /*-------------------------------------------------------------------------------------------------------*/
    31.  
    32. }
    33.  
    34. }// end of namespace PFT3
    To copy to clipboard, switch view to plain text mode 

    Then I write on QtScript:

    Qt Code:
    1. //delete task;
    2. var task = new PFT3_Tasks.BaseData(1, 2, 6000, 2000, true);
    3. pft3TaskEngine.appendTask(task);
    To copy to clipboard, switch view to plain text mode 

    And get script exception,that I parse to output:
    Error in script caused the exception:
    "TypeError: cannot call appendTask(): argument 1 has unknown type `PFT3_Tasks::BaseData*' (register the type with qScriptRegisterMetaType())"
    at executing line 3.
    Call stack:
    1. <anonymous>()@Script, entered by user:3
    http://lists.qt.nokia.com/public/qt-...ry/002051.html I read and tried with typedef.
    Making Applications Scriptable I read too and many times.
    How to do this correctly?
    Thank you for your replies.

  2. #2
    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 register pointer type in qScriptRegisterMetatype?

    Couple of points:

    1) I would use QScriptEngine::newObject() instead of newQObject() for the namespace

    2) I would not use a QObject subclass for the data entry if possible, only the prototype has to be. You can then pass BaseData as value and const reference

    3) Since you are using a prototype, you don't need conversion functions and you don't need to call qScriptRegisterMetaType()

    You basically do something like this
    Qt Code:
    1. const int typeId = qRegisterMetaType<PFT3::PFT3_Tasks::BaseData*>("PFT3::PFT3_Tasks::BaseData*");
    2. QScriptValue prototype = engine->newQObject(new PFT3_Tasks::BaseDataScriptProto(engine));
    3. engine->setDefaultPrototype(typeId, prototype);
    4. engine->setDefaultPrototype(qMetaTypeId<PFT3::PFT3_Tasks::BaseData>(), prototype);
    5.  
    6. QScriptValue namespaceObj = engine->newObject();
    7. engine->globalObject().setProperty("PFT3_Tasks", namespaceObj);
    8.  
    9. QScriptValue creator = engine->newFunction(createData, prototype);
    10. namespaceObj.setProperty("BaseData", creator);
    To copy to clipboard, switch view to plain text mode 

    In functions of the prototype class you then access the data objects like this
    Qt Code:
    1. qscriptvalue_cast<BaseData*>(thisObject());
    To copy to clipboard, switch view to plain text mode 

    The constructor function would look like this
    Qt Code:
    1. QScriptValue createData(QScriptContext *context, QScriptEngine *engine)
    2. {
    3. if (context->isCalledAsConstructor()) {
    4. BaseData *data = new BaseData;
    5. return engine->toScriptValue(data);
    6. } else {
    7. return engine->undefinedValue();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    Sacha_D (24th April 2013)

  4. #3
    Join Date
    Aug 2012
    Location
    USSR
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to register pointer type in qScriptRegisterMetatype?

    Thank you.
    I added this 2 lines to my code and it began to work.

    Qt Code:
    1. const int typeId = qRegisterMetaType<PFT3::PFT3_Tasks::BaseData*>("PFT3_Tasks::BaseData*");
    2. scriptEngine.setDefaultPrototype(typeId, baseDataProto);
    To copy to clipboard, switch view to plain text mode 

    But why it shows me the same error if I write like you advised to me?
    Qt Code:
    1. const int typeId = qRegisterMetaType<PFT3::PFT3_Tasks::BaseData*>("PFT3::PFT3_Tasks::BaseData*");
    To copy to clipboard, switch view to plain text mode 

    PFT3_Task is in namespace PFT3.

    Same error
    "TypeError: cannot call appendTask(): argument 1 has unknown type `PFT3_Tasks::BaseData*' (register the type with qScriptRegisterMetaType())"
    I have got when BaseData in script was in globalObject direcly before I created this topic.

    I want want to understand: "Why it not needs PFT3::"?

  5. #4
    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 register pointer type in qScriptRegisterMetatype?

    Good question.
    Do you have a "using namespace PFT3" clause somewhere?

    Maybe try to call qRegisterMetaType without any argument, i.e. only specify the template argument but no value inside the parentheses.

    Cheers,
    _

Similar Threads

  1. Qt Creator PC register is not available
    By okaresz in forum Qt Tools
    Replies: 0
    Last Post: 11th February 2013, 22:21
  2. What is register int?
    By kiboi in forum General Programming
    Replies: 2
    Last Post: 7th November 2012, 00:27
  3. Can I cast a pointer from a unknow type ?
    By john_god in forum General Programming
    Replies: 13
    Last Post: 30th May 2011, 13:59
  4. Pointer of an unknown type...
    By TheNewGuy in forum Newbie
    Replies: 1
    Last Post: 17th December 2009, 09:50
  5. Dynamic pointer type
    By estanisgeyer in forum General Programming
    Replies: 3
    Last Post: 9th October 2008, 17:51

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.