Results 1 to 4 of 4

Thread: QtScript binding wrappers? An Easy Way?

  1. #1
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QtScript binding wrappers? An Easy Way?

    I've been struggling with figuring out how to wrap the QtScript bindings (or anything else for that matter) so that the base class properties and slots are still visible to my newly created QtScript object.

    For example in QtScript using bindings created by the QtBindingsGenerator I can do the following:
    Qt Code:
    1. var page = new QWizardPage();
    2. var layout = new QGridLayout();
    3. page.setLayout(layout); //setLayout is actually defined in QWidget and it is accessible here
    To copy to clipboard, switch view to plain text mode 
    However I need access to 'protected' functionality of QWizardPage so I've been trying to create a wrapper to expose these methods without removing existing things I can call from QtScript.
    Qt Code:
    1. class ScriptWizardPage : public QWizardPage
    2. {
    3. Q_OBJECT;
    4. ...
    5. public:
    6. Q_INVOKABLE void registerField(...);
    7. };
    8. Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptWizardPage, QWidget*);
    9.  
    10. //Adding to engine
    11. QScriptValue newClass = engine->scriptValueFromQMetaObject(ScriptWizardPage>();
    12. engine->globalObject().setProperty("ScriptWizardPage", newClass);
    To copy to clipboard, switch view to plain text mode 

    By creating the ScriptWizardPage i can now access the 'registerField' method in QtScript however all of the previously exposed methods such as setLayout() are removed and I am unaware of how to keep them there without completely wrapping all functionality of a QWizardPage all the way down to QWidget -- there must be a better, less tedious way that eludes me!?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript binding wrappers? An Easy Way?

    Hi there!

    Are you aware of the scriptshell? There is a good chance that your protected method that you want to expose is already exposed.

    look into .. /QtScriptBindingsGenerator/generated_cpp/com_trolltech_qt_gui/qtscriptshell_QWizardPage.cpp

    you just create a function property inside QtScript with the same name, that is then called.

    If you also want to call the implementation of the base class have a look at my post regarding this:

    http://www.qtcentre.org/threads/29044-QtScript-Bindings-Generator-ScriptShell-Event-Binding

    If all that doesn't work for you.. you could try to assign a QWizardPage Scriptobject as prototype to yours.

    Johannes

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

    Statix (27th June 2010)

  4. #3
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript binding wrappers? An Easy Way?

    I checked in the QWizardPage.cpp for that same file and didn't find the method I wanted (specifically http://doc.qt.nokia.com/4.6/qwizardp...#registerField and its friends).

    I don't think i can change the binding generator due to its license restrictions on my project so I think my last method is the ' QWizardPage Scriptobject as prototype' suggestion however that is the part I'm most unclear on.

    In order to do that I need to setDefaultPrototype before exposing my ScriptWizardPage wrapper to QtScript or afterward in the code (not ideal for me).

    Can you elaborate? I haven't been able to entirely grasp prototypes in QtScript

  5. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript binding wrappers? An Easy Way?

    I never actually had to do it before either, but have a look at how the bindings do it:

    excerpt from qtscript_QWizardPage.cpp:
    Qt Code:
    1. QScriptValue qtscript_create_QWizardPage_class(QScriptEngine *engine)
    2. {
    3. ...
    4. engine->setDefaultPrototype(qMetaTypeId<QWizardPage*>(), QScriptValue());
    5. QScriptValue proto = engine->newVariant(qVariantFromValue((QWizardPage*)0));
    6. proto.setPrototype(engine->defaultPrototype(qMetaTypeId<QWidget*>()));
    7. for (int i = 0; i < 14; ++i) {
    8. QScriptValue fun = engine->newFunction(qtscript_QWizardPage_prototype_call, function_lengths[i+1]);
    9. fun.setData(QScriptValue(engine, uint(0xBABE0000 + i)));
    10. proto.setProperty(QString::fromLatin1(qtscript_QWizardPage_function_names[i+1]),
    11. fun, QScriptValue::SkipInEnumeration);
    12. }
    13.  
    14. qScriptRegisterMetaType<QWizardPage*>(engine, qtscript_QWizardPage_toScriptValue,
    15. qtscript_QWizardPage_fromScriptValue, proto);
    To copy to clipboard, switch view to plain text mode 

    You need to get the prototype of the QWizardPage, set it as prototype of your own class and add the function calls you need.

    HIH

    Joh

Similar Threads

  1. QtScript Bindings Generator ScriptShell Event Binding
    By JohannesMunk in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2010, 13:10
  2. Replies: 0
    Last Post: 25th November 2009, 07:46
  3. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  4. QTextEdit... was ->text() too easy?
    By Raccoon29 in forum Newbie
    Replies: 2
    Last Post: 16th March 2008, 16:58
  5. binding
    By mickey in forum General Discussion
    Replies: 9
    Last Post: 26th September 2006, 21:54

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.