Results 1 to 4 of 4

Thread: QtScript Binding problem with QWidget

  1. #1
    Join Date
    Feb 2009
    Posts
    20

    Default QtScript Binding problem with QWidget

    I want to add a QWidget for QtScript.

    But unfortunately when I debug my protoype, I get no calls to my properties. And also all other properties from a QWidget are visible and accessible (I can see this, because I use the QtScript debugger)

    I do it like the following code:

    Qt Code:
    1. class WidgetPrototype : public QObject, protected QScriptable
    2. {
    3. Q_OBJECT
    4.  
    5. Q_PROPERTY(QString whatsThis READ whatsThis WRITE setWhatsThis)
    6. Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip)
    7.  
    8. public:
    9. WidgetPrototype(QObject *parent = 0);
    10.  
    11. QString whatsThis() const;
    12. void setWhatsThis(const QString &text) const;
    13. QString toolTip() const;
    14. void setToolTip(const QString &text) const;
    15.  
    16. static QScriptValue construct(QScriptContext *ctx, QScriptEngine *eng);
    17.  
    18. private:
    19. QWidget *thisWidget() const;
    20. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Q_DECLARE_METATYPE(QWidget*)
    2.  
    3. WidgetPrototype::WidgetPrototype(QObject *parent)
    4. : QObject(parent)
    5. {
    6.  
    7. }
    8.  
    9. QScriptValue WidgetPrototype::construct(QScriptContext *ctx, QScriptEngine *eng)
    10. {
    11. if(ctx->argumentCount()==0)
    12. {
    13. return eng->toScriptValue(new QWidget());
    14. }
    15.  
    16. return QScriptValue();
    17. }
    18.  
    19. QString WidgetPrototype::whatsThis() const
    20. {
    21. if(thisWidget())
    22. return thisWidget()->whatsThis();
    23. else
    24. return "";
    25. }
    26.  
    27. void WidgetPrototype::setWhatsThis(const QString &text) const
    28. {
    29. if(thisWidget())
    30. thisWidget()->setWhatsThis(text);
    31. }
    32.  
    33. QString WidgetPrototype::toolTip() const
    34. {
    35. if(thisWidget())
    36. return thisWidget()->toolTip();
    37. else
    38. return "";
    39. }
    40.  
    41. void WidgetPrototype::setToolTip(const QString &text) const
    42. {
    43. if(thisWidget())
    44. thisWidget()->setToolTip(text);
    45. }
    46.  
    47. QWidget *WidgetPrototype::thisWidget() const
    48. {
    49. return qscriptvalue_cast<QWidget*>(thisObject());
    50. }
    To copy to clipboard, switch view to plain text mode 

    I create the binding with QtScript engine:
    Qt Code:
    1. WidgetPrototype WidgetProtoObject();
    2.  
    3. QScriptValue widgetProto = engine.newQObject(&WidgetProtoObject);
    4. engine.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);
    5. QScriptValue widgetCtor = engine.newFunction(WidgetProtoObject.construct, widgetProto);
    6. engine.globalObject().setProperty("Widget", widgetCtor);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. var tem = new Widget();
    2. tem.whatsThis="asdcvbs";
    3. print(tem.whatsThis); // the script engine prints out "asdcvbs", but no from my whatsThis function in WidgetPrototype
    4. print(tem.isActiveWindow); // I didnt add a slot for this property, but I get no mistake
    To copy to clipboard, switch view to plain text mode 

    Does anybody know where the mistake is?
    Last edited by zack; 16th February 2009 at 17:13.

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtScript Binding problem with QWidget

    One difference to my working prototypes is the constructor. Translated to your class it would be:

    Qt Code:
    1. QScriptValue WidgetPrototype::construct(QScriptContext *ctx, QScriptEngine *eng)
    2. {
    3. if(ctx->argumentCount()==0)
    4. {
    5. // use newQObject; script ownership
    6. return eng->newQObject(new QWidget(), QScriptEngine::ScriptOwnership);
    7. }
    8.  
    9. return eng->undefinedValue();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Other differences:

    Qt Code:
    1. // prototype object not on stack:
    2. WidgetPrototype* WidgetPrototypeObject = new WidgetPrototype();
    3.  
    4. // make it script owned:
    5. QScriptValue widgetProto = engine.newQObject(WidgetPrototypeObject, QScriptEngine::ScriptOwnership);
    6.  
    7. engine.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);
    8. QScriptValue widgetCtor = engine.newFunction(WidgetPrototype::construct, widgetProto);
    9. engine.globalObject().setProperty("Widget", widgetCtor);
    To copy to clipboard, switch view to plain text mode 
    Last edited by seneca; 17th February 2009 at 08:35.

  3. #3
    Join Date
    Feb 2009
    Posts
    20

    Default Re: QtScript Binding problem with QWidget

    Did you test you WidgetProtoype with the QtScriptDebugger?

    With your changes I have still all properties of a QWidget available and it does not call my prototype functions, when I call e.g. setWhatsThis()..

  4. #4
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtScript Binding problem with QWidget

    I didn't test your class, but mine work as expected.

    Wild guess, would this maybe make a difference:

    Qt Code:
    1. var tem = new Widget;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Problem with Size Policy of derived QWidget
    By eehmke in forum Qt Programming
    Replies: 6
    Last Post: 12th November 2008, 13:43
  2. QWidget Refresh Problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2008, 11:24
  3. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 16:07
  4. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59

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.