Results 1 to 16 of 16

Thread: Custom QObjet in QScript

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.


  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

    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.

  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

    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.