Results 1 to 2 of 2

Thread: Qt script return pointer

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Qt script return pointer

    Hey there,

    I'm trying to achieve the following using QtScript:

    Qt Code:
    1. test.prototype.createPage = function(factory)
    2. {
    3. test = factory.newPicture("box.png");
    4. }
    To copy to clipboard, switch view to plain text mode 

    The newPicture interface is correctly binded and returns a qkPicture *.
    The problem is, due to non typed variables, QtScript assumes he has to copy to content of the qkPicture.

    Is there a way to emulate pointer behaviour and dereference test ?
    Or a way to bypass non typed scripting syntax ?

    Thanks.
    Benjamin Arnaud.

  2. #2
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Qt script return pointer

    Returning QObject Pointers

    If you have a slot that returns a QObject pointer, you should note that, by default, Qt Script only handles conversion of the types QObject* and QWidget*. This means that if your slot is declared with a signature like "MyObject* getMyObject()", QtScript doesn't automatically know that MyObject* should be handled in the same way as QObject* and QWidget*. The simplest way to solve this is to only use QObject* and QWidget* in the method signatures of your scripting interface.

    Alternatively, you can register conversion functions for your custom type with the qScriptRegisterMetaType() function. In this way, you can preserve the precise typing in your C++ declarations, while still allowing pointers to your custom objects to flow seamlessly between C++ and scripts. Example:

    class MyObject : public QObject
    {
    Q_OBJECT
    ...
    };

    Q_DECLARE_METATYPE(MyObject*)

    QScriptValue myObjectToScriptValue(QScriptEngine *engine, MyObject* const &in)
    { return engine->newQObject(in); }

    void myObjectFromScriptValue(const QScriptValue &object, MyObject* &out)
    { out = qobject_cast<MyObject*>(object.toQObject()); }

    ...

    qScriptRegisterMetaType(&engine, myObjectToScriptValue, myObjectFromScriptValue);
    http://doc.trolltech.com/main-snapsh...ed-inheritance

Similar Threads

  1. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  2. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  3. Possible signal mapper problem
    By MarkoSan in forum Qt Programming
    Replies: 13
    Last Post: 25th January 2008, 13:11
  4. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 10:44
  5. QTableView Repaint/Refresh
    By millsks in forum Newbie
    Replies: 9
    Last Post: 10th January 2008, 17:18

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.