Results 1 to 16 of 16

Thread: Custom QObjet in QScript

  1. #1
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Custom QObjet in QScript

    Hi everybody.

    I've declare a custom QNetworkManager

    Qt Code:
    1. class NetworkManager : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit NetworkManager(QObject *parent = 0);
    6. NetworkManager(const NetworkRequest &);
    7. virtual ~NetworkManager();
    8.  
    9. Q_INVOKABLE NetworkReply get(NetworkRequest request);
    10. Q_INVOKABLE NetworkReply head(NetworkRequest request);
    11. Q_INVOKABLE NetworkReply post(NetworkRequest request);
    12. Q_INVOKABLE NetworkReply put(NetworkRequest request);
    13.  
    14. private:
    15. QNetworkAccessManager m_manager;
    16. };
    17.  
    18. class NetworkRequest : public QObject
    19. {
    20. Q_OBJECT
    21. public:
    22. explicit NetworkRequest(QUrl par_url, QObject *parent = 0);
    23. NetworkRequest(const NetworkRequest &);
    24. virtual ~NetworkRequest();
    25.  
    26. };
    27.  
    28. class NetworkReply : public QObject
    29. {
    30. Q_OBJECT
    31. public:
    32. NetworkReply(QObject *parent = 0);
    33. explicit NetworkReply(QNetworkReply *par_reply, QObject *parent = 0);
    34. NetworkReply(const NetworkReply &);
    35. virtual ~NetworkReply();
    36. private:
    37. QNetworkReply *m_reply;
    38.  
    39. };
    To copy to clipboard, switch view to plain text mode 

    but when in script I do this :
    Qt Code:
    1. var Request = NetworkRequest("www.google.fr");
    2. var Manager = NetworkManager();
    3. var Respond = Manager.get(Request);
    To copy to clipboard, switch view to plain text mode 

    I obtain this message :
    Qt Code:
    1. TypeError: cannot call get(): unknown return type `NetworkReply' (register the type with qScriptRegisterMetaType())
    2. get(NetworkRequest(name = ""))@:-1
    To copy to clipboard, switch view to plain text mode 

    so I add in the .h
    Qt Code:
    1. Q_DECLARE_METATYPE(NetworkReply*)
    2. QScriptValue NetworkReplyToScriptValue(QScriptEngine *engine, NetworkReply* const &in);
    3. void NetworkReplyFromScriptValue(const QScriptValue &object, NetworkReply* &out);
    4. Q_DECLARE_METATYPE(NetworkRequest*)
    5. QScriptValue NetworkRequestToScriptValue(QScriptEngine *engine, NetworkRequest* const &in);
    6. void NetworkRequestFromScriptValue(const QScriptValue &object, NetworkRequest* &out);
    To copy to clipboard, switch view to plain text mode 
    and in cpp
    Qt Code:
    1. QScriptValue NetworkRequestToScriptValue(QScriptEngine *engine, NetworkRequest* const &in) { return engine->newQObject(in); }
    2. void NetworkRequestFromScriptValue(const QScriptValue &object, NetworkRequest* &out) { out = qobject_cast<NetworkRequest*>(object.toQObject()); }
    3.  
    4. QScriptValue NetworkReplyToScriptValue(QScriptEngine *engine, NetworkReply* const &in) { return engine->newQObject(in); }
    5. void NetworkReplyFromScriptValue(const QScriptValue &object, NetworkReply* &out) { out = qobject_cast<NetworkReply*>(object.toQObject()); }
    6.  
    7. void NetworkRequestRegister::Register(QScriptEngine *engine)
    8. {
    9. par_engine->globalObject().setProperty("NetworkRequest", par_engine->newFunction(createNetworkRequest));
    10. par_engine->globalObject().setProperty("NetworkManager", par_engine->newFunction(createNetworkManager));
    11. par_engine->globalObject().setProperty("NetworkReply", par_engine->newFunction(createNetworkReply));
    12. qScriptRegisterMetaType(engine, NetworkReplyToScriptValue, NetworkReplyFromScriptValue);
    13. qScriptRegisterMetaType(engine, NetworkRequestToScriptValue, NetworkRequestFromScriptValue);
    14. }
    To copy to clipboard, switch view to plain text mode 

    But the error is the same.
    Somebody know why ?

    Thanks in advance.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Custom QObjet in QScript

    What happens if you replace the third "var" with "NetworkReply"?
    Last edited by Urthas; 31st August 2010 at 21:52.

  3. #3
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    Qt Code:
    1. var Request = NetworkRequest("www.google.fr");
    2. var Manager = NetworkManager();
    3. NetworkReply Respond = Manager.get(Request);
    To copy to clipboard, switch view to plain text mode 

    it says there is a malformed script when I use QScriptSyntaxCheckResult to check the syntax, a ';' is missing.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QObjet in QScript

    You are registering "NetworkReply*" but using "NetworkReply" which is not registered with the metatype system.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    Thanks
    But I add this, and the error is the same :
    Qt Code:
    1. Q_DECLARE_METATYPE(NetworkReply)
    2.  
    3. QScriptValue NetworkReplyToScriptValue2(QScriptEngine *engine, NetworkReply const &in) { return engine->newQObject((QObject *)&in); }
    4. void NetworkReplyFromScriptValue2(const QScriptValue &object, NetworkReply &out) { out = (NetworkReply)(object.toQObject()); }
    5.  
    6. ...
    7.  
    8. qScriptRegisterMetaType(engine, NetworkReplyToScriptValue2, NetworkReplyFromScriptValue2);
    To copy to clipboard, switch view to plain text mode 

    What I don't understand ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QObjet in QScript

    Please provide a minimal compilable example reproducing the problem. By the way, you know QObjects can't be copied, right? So deriving NetworkReply from QObject probably doesn't make much sense...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    That's why I add NetworkReply(const NetworkReply &); (if I don't understand everything, it's because I don't often speak English, sorry)

    The problem is really when I try to use a custom QObject in other via QScript

    I try to add minimal example but I don't compile it :

    Qt Code:
    1. #ifndef NETWORKREQUEST_H
    2. #define NETWORKREQUEST_H
    3.  
    4. #include <QObject>
    5. #include <QScriptEngine>
    6.  
    7. #include <QNetworkReply>
    8. #include <QNetworkAccessManager>
    9. #include <QNetworkRequest>
    10.  
    11. class NetworkRequestRegister
    12. {
    13. public:
    14. static void Register(QScriptEngine *engine);
    15. };
    16.  
    17. class NetworkRequest : public QObject
    18. {
    19. Q_OBJECT
    20. public:
    21. explicit NetworkRequest(QUrl url, QObject *parent = 0);
    22. NetworkRequest(const NetworkRequest &);
    23. virtual ~NetworkRequest();
    24.  
    25. QNetworkRequest getRequest() { return request; }
    26.  
    27. private:
    28. QNetworkRequest request;
    29.  
    30. };
    31.  
    32. class NetworkReply;
    33. class NetworkManager : public QObject
    34. {
    35. Q_OBJECT
    36. public:
    37. explicit NetworkManager(QObject *parent = 0);
    38. NetworkManager(const NetworkRequest &);
    39. virtual ~NetworkManager();
    40.  
    41. QNetworkReply *Qget(NetworkRequest request);
    42. Q_INVOKABLE NetworkReply get(NetworkRequest request);
    43.  
    44. private:
    45. QNetworkAccessManager manager;
    46. };
    47.  
    48. class NetworkReply : public QObject
    49. {
    50. Q_OBJECT
    51. public:
    52. NetworkReply(QObject *parent = 0);
    53. explicit NetworkReply(QNetworkReply *par_reply, QObject *parent = 0);
    54. NetworkReply(const NetworkReply &);
    55. virtual ~NetworkReply();
    56.  
    57. NetworkReply &operator =(const NetworkReply &);
    58.  
    59. private:
    60. QNetworkReply *reply;
    61.  
    62. };
    63.  
    64. Q_SCRIPT_DECLARE_QMETAOBJECT(NetworkReply, QObject*);
    65. Q_DECLARE_METATYPE(NetworkReply)
    66. Q_DECLARE_METATYPE(NetworkReply*)
    67. QScriptValue NetworkReplyToScriptValue(QScriptEngine *engine, NetworkReply* const &in);
    68. void NetworkReplyFromScriptValue(const QScriptValue &object, NetworkReply* &out);
    69. Q_DECLARE_METATYPE(NetworkRequest*)
    70. QScriptValue NetworkRequestToScriptValue(QScriptEngine *engine, NetworkRequest* const &in);
    71. void NetworkRequestFromScriptValue(const QScriptValue &object, NetworkRequest* &out);
    72.  
    73. #endif // NETWORKREQUEST_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <math.h>
    2.  
    3. #include "networkrequest.h"
    4.  
    5. QScriptValue createNetworkRequest(QScriptContext *context, QScriptEngine *engine)
    6. {
    7. if(par_context->argumentCount() != 1 && context->argument(0).isString())
    8. {
    9. return engine->nullValue();
    10. }
    11.  
    12. NetworkRequest *loc_request = new NetworkRequest(QUrl(context->argument(0).toString()), engine);
    13. return par_engine->newQObject(request);
    14. }
    15.  
    16. QScriptValue createNetworkManager(QScriptContext *context, QScriptEngine *engine)
    17. {
    18. if(par_context->argumentCount() != 0)
    19. {
    20. return engine->nullValue();
    21. }
    22.  
    23. NetworkManager *manager = new NetworkManager(engine);
    24. return engine->newQObject(manager);
    25. }
    26.  
    27. QScriptValue NetworkRequestToScriptValue(QScriptEngine *engine, NetworkRequest* const &in) { return engine->newQObject(in); }
    28. void NetworkRequestFromScriptValue(const QScriptValue &object, NetworkRequest* &out) { out = qobject_cast<NetworkRequest*>(object.toQObject()); }
    29.  
    30. QScriptValue NetworkReplyToScriptValue(QScriptEngine *engine, NetworkReply* const &in) { return engine->newQObject(in); }
    31. void NetworkReplyFromScriptValue(const QScriptValue &object, NetworkReply* &out) { out = qobject_cast<NetworkReply*>(object.toQObject()); }
    32.  
    33. QScriptValue NetworkReplyToScriptValue2(QScriptEngine *engine, NetworkReply const &in) { return engine->newQObject((QObject *)&in); }
    34. void NetworkReplyFromScriptValue2(const QScriptValue &object, NetworkReply &out) { out = (NetworkReply)(object.toQObject()); }
    35.  
    36. QScriptValue createNetworkReply(QScriptContext *context, QScriptEngine *engine)
    37. {
    38. if(context->argumentCount() != 0)
    39. {
    40. return engine->nullValue();
    41. }
    42.  
    43. NetworkReply *manager = new NetworkReply(engine);
    44. return engine->newQObject(manager);
    45. }
    46.  
    47. void NetworkRequestRegister::Register(QScriptEngine *engine)
    48. {
    49. par_engine->globalObject().setProperty("NetworkRequest", engine->newFunction(createNetworkRequest));
    50. par_engine->globalObject().setProperty("NetworkManager", engine->newFunction(createNetworkManager));
    51. par_engine->globalObject().setProperty("NetworkReply", engine->newFunction(createNetworkReply));
    52. qScriptRegisterMetaType(engine, NetworkReplyToScriptValue, NetworkReplyFromScriptValue);
    53. qScriptRegisterMetaType(engine, NetworkReplyToScriptValue2, NetworkReplyFromScriptValue2);
    54. qScriptRegisterMetaType(engine, NetworkRequestToScriptValue, NetworkRequestFromScriptValue);
    55. }
    56.  
    57. NetworkRequest::NetworkRequest(QUrl url, QObject *parent) :
    58. QObject(parent),
    59. request(QNetworkRequest(url))
    60. {
    61. }
    62.  
    63. NetworkRequest::NetworkRequest(const NetworkRequest &request)
    64. {
    65. m_request = par_request.request;
    66. }
    67.  
    68. NetworkRequest::~NetworkRequest()
    69. {
    70. file->close();
    71. delete file;
    72. }
    73.  
    74. NetworkReply::NetworkReply(QNetworkReply *reply, QObject *parent) : QObject(parent)
    75. {
    76. reply = reply;
    77. if(!reply->open(QIODevice::ReadOnly))
    78. qDebug(QString("Network Reply > cannot open reply IO Device").toLatin1());
    79. }
    80.  
    81. NetworkReply::NetworkReply(QObject *parent) : QObject(parent)
    82. {
    83. }
    84.  
    85. NetworkReply::NetworkReply(const NetworkReply &reply)
    86. {
    87. reply = reply.reply;
    88. }
    89.  
    90. NetworkReply::~NetworkReply()
    91. {
    92. //reply->close();
    93. //delete reply;
    94. }
    95.  
    96. NetworkReply &NetworkReply::operator =(const NetworkReply &reply)
    97. {
    98. return NetworkReply(reply);
    99. }
    100.  
    101. NetworkManager::NetworkManager(QObject *parent) : QObject(parent), manager(QNetworkAccessManager(parent))
    102. {
    103. }
    104.  
    105. NetworkManager::~NetworkManager()
    106. {
    107. }
    108.  
    109. QNetworkReply *NetworkManager::Qget(NetworkRequest request)
    110. {
    111. return m_manager.get(request.getRequest());
    112. }
    113.  
    114. NetworkReply NetworkManager::get(NetworkRequest request)
    115. {
    116. return NetworkReply(m_manager.get(request.getRequest()), parent());
    117. }
    To copy to clipboard, switch view to plain text mode 

    in the main
    Qt Code:
    1. QFile *file = new QFile("scenario.txt");
    2. file->open(QIODevice::ReadOnly);
    3. QString scenario = file->readAll();
    4. QScriptEngine *engine = new QScriptEngine(parent);
    5. NetworkRequestRegister::Register(engine);
    6.  
    7. QScriptSyntaxCheckResult checkResult = QScriptEngine::checkSyntax(screnario);
    8. if(checkResult.state() == QScriptSyntaxCheckResult::Valid)
    9. {
    10. engine->evaluate(screnario);
    11. }
    To copy to clipboard, switch view to plain text mode 

    And in scenatio.txt

    Qt Code:
    1. function fileGetTest() {
    2. var Request = NetworkRequest("www.google.fr");
    3. var Manager = NetworkManager();
    4. var Respond = Manager.get(Request);
    5. }
    6.  
    7. fileGetTest();
    To copy to clipboard, switch view to plain text mode 

    There is a way to add a file in a post ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QObjet in QScript

    Quote Originally Posted by Ben1 View Post
    That's why I add NetworkReply(const NetworkReply &);
    That doesn't change anything. You return copies of QObject from your NetworkManager class. I don't know why you made NetworkRequest and NetworkReply descendants of QObject anyway - you are not using any of QObject features in these classes.

    I try to add minimal example but I don't compile it :
    Minimal is "less than 30 lines of code long".

    There is a way to add a file in a post ?
    Yes, of course. "Go Advanced" and click on the paper clip icon.

    According to me you are overcomplicating things. Focus on your goal and not on QObjects. None of your classes have to be QObjects. If you are deriving them from QObject just to use Q_INVOKABLE then your NetworkManager should return pointers to NetworkReply and NetworkRequest. Also you have to tell QtScript how to create instances of NetworkRequest and NetworkReply.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    I use QObject because it seems a simplest way to use object in QtScript (and I don't really understand the documentation). How can I do ?
    I have the problem to return myC++Object and use it in parameter.
    Use of pointer, like this Q_INVOKABLE NetworkReply *get(NetworkRequest request); doesn't work.

    Sorry for my (non-)minimal code !

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QObjet in QScript

    "It doesn't work" statement is not very useful

    Not all simplest (or requiring the least work) solutions are good solutions. If you have a value-based class (such as (Q)NetworkRequest) with only a few methods, there are better approaches to use it than going through QObject.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    Ben1 (1st September 2010)

  12. #11
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    The "it doesn't work" means the error is always
    Qt Code:
    1. "TypeError: cannot call get(): argument 1 has unknown type `NetworkRequest' (register the type with qScriptRegisterMetaType()) : line => 66
    2. get(NetworkRequest(name = ""))@:-1
    3. fileGetTest()@:66
    4. <global>()@:85
    To copy to clipboard, switch view to plain text mode 

    but change to
    Qt Code:
    1. Q_INVOKABLE NetworkReply *get(NetworkRequest *request);
    To copy to clipboard, switch view to plain text mode 
    doesn't make an error like the above. I can't get my page but it seems to be an other problem.

    I do this quickly to prove to my colleague it is possible to use QtScript but I'm a beginner in QtScript.
    Thank you for your help.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Unhappy Re: Custom QObjet in QScript

    Let's start with the fact that you don't need NetworkRequest at all. All it does in your case is that it carries a string. But if I'm correct at what you are trying to accomplish then you don't need any of the three classes at all. You just need wrappers for them in QtScript environment. You could do it with i.e. QScriptClass or two simple converter functions per class.

    Edit: here is something similar to what you wanted:
    Qt Code:
    1. #include <QtScript>
    2. #include <QtGui>
    3.  
    4. Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
    5.  
    6.  
    7. int main(int argc, char **argv){
    8. QApplication app(argc, argv);
    9. QScriptEngine engine;
    10. QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
    11. engine.globalObject().setProperty("QLineEdit", lineEditClass);
    12. QScriptValue val = engine.evaluate("var le = new QLineEdit; le.show();");
    13. if(val.isError())
    14. qDebug() << val.toString();
    15. app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 1st September 2010 at 15:16.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    Ben1 (1st September 2010)

  15. #13
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    What I need if I want to use my own class (no a Qt class)

    example :
    Qt Code:
    1. class MyObj
    2. {
    3. MyObj();
    4. ~ MyObj();
    5. void calculate() { 3 + 5;}
    6. }
    To copy to clipboard, switch view to plain text mode 

    I just have to use Q_SCRIPT_DECLARE_QMETAOBJECT(MyObj, QObject*) and

    Qt Code:
    1. int main(int argc, char **argv){
    2. QApplication app(argc, argv);
    3. QScriptEngine engine;
    4. QScriptValue MyObjClass = engine.scriptValueFromQMetaObject<MyObj>();
    5. engine.globalObject().setProperty("MyObj", MyObjClass );
    6. QScriptValue val = engine.evaluate("var le = new MyObj; le.calculate();");
    7. if(val.isError())
    8. qDebug() << val.toString();
    9. app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    It's just to be sure what I can do.

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QObjet in QScript

    First you can open the docs and read why QScriptEngine::scriptValueFromQMetaObject() is not suitable for your MyObj class.

    Qt Code:
    1. class MyObj : public QObject {
    2. Q_OBJECT
    3. public:
    4. MyObj(){}
    5. ~MyObj(){}
    6. public slots:
    7. void calculate() { qDebug() << Q_FUNC_INFO; }
    8. };
    9.  
    10. #include "main.moc"
    11.  
    12. int main(int argc, char **argv){
    13. QScriptEngine engine;
    14. QScriptValue MyObjClass = engine.scriptValueFromQMetaObject<MyObj>();
    15. engine.globalObject().setProperty("MyObj", MyObjClass );
    16. QScriptValue val = engine.evaluate("var le = new MyObj; le.calculate();");
    17. if(val.isError())
    18. qDebug() << val.toString();
    19. }
    To copy to clipboard, switch view to plain text mode 

    or use QScriptClass or QScriptable to avoid QObject legacy. You can also use one of the prototype based approaches, i.e.:

    Qt Code:
    1. QScriptValue MyObj_ctor(QScriptContext *context, QScriptEngine *engine) {
    2. return engine->undefinedValue();
    3. }
    4.  
    5. QScriptValue MyObj_prototype_calculate(QScriptContext *context, QScriptEngine *engine)
    6. {
    7. qDebug() << "calculate";
    8. return engine->undefinedValue();
    9. }
    10.  
    11. // ...
    12.  
    13. QScriptEngine engine;
    14. QScriptValue ctor = engine.newFunction(MyObj_ctor);
    15. ctor.property("prototype").setProperty("calculate", engine.newFunction(MyObj_prototype_calculate));
    16. QScriptValue global = engine.globalObject();
    17. global.setProperty("MyObj", ctor);
    To copy to clipboard, switch view to plain text mode 

    What you choose depends on how you intend to use the class.
    Last edited by wysota; 1st September 2010 at 15:54.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #15
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Custom QObjet in QScript

    The problem is I don't really know how to use my class. I try to provide a framework that can be used by users who can write QtScript using this framework.

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom QObjet in QScript

    Quote Originally Posted by Ben1 View Post
    The problem is I don't really know how to use my class.
    I don't know how to use your class too. You'll probably be the first to know how to use your class. If not then it's probably not a good idea to dwell about the problem at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to use QScript?
    By lni in forum Qt Programming
    Replies: 5
    Last Post: 2nd August 2010, 14:36
  2. qscript suspend function?
    By reneb in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2010, 13:03
  3. QScript + SimpleClass not working
    By tdt in forum Qt Programming
    Replies: 0
    Last Post: 14th February 2010, 08:25
  4. QScript workbench in 4.3
    By jh in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2009, 13:53
  5. QVariantList from QScript?
    By musla in forum Qt Programming
    Replies: 0
    Last Post: 29th June 2009, 10:22

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.