PDA

View Full Version : QtScript Binding problem with QWidget



zack
16th February 2009, 18:07
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:



class WidgetPrototype : public QObject, protected QScriptable
{
Q_OBJECT

Q_PROPERTY(QString whatsThis READ whatsThis WRITE setWhatsThis)
Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip)

public:
WidgetPrototype(QObject *parent = 0);

QString whatsThis() const;
void setWhatsThis(const QString &text) const;
QString toolTip() const;
void setToolTip(const QString &text) const;

static QScriptValue construct(QScriptContext *ctx, QScriptEngine *eng);

private:
QWidget *thisWidget() const;
};




Q_DECLARE_METATYPE(QWidget*)

WidgetPrototype::WidgetPrototype(QObject *parent)
: QObject(parent)
{

}

QScriptValue WidgetPrototype::construct(QScriptContext *ctx, QScriptEngine *eng)
{
if(ctx->argumentCount()==0)
{
return eng->toScriptValue(new QWidget());
}

return QScriptValue();
}

QString WidgetPrototype::whatsThis() const
{
if(thisWidget())
return thisWidget()->whatsThis();
else
return "";
}

void WidgetPrototype::setWhatsThis(const QString &text) const
{
if(thisWidget())
thisWidget()->setWhatsThis(text);
}

QString WidgetPrototype::toolTip() const
{
if(thisWidget())
return thisWidget()->toolTip();
else
return "";
}

void WidgetPrototype::setToolTip(const QString &text) const
{
if(thisWidget())
thisWidget()->setToolTip(text);
}

QWidget *WidgetPrototype::thisWidget() const
{
return qscriptvalue_cast<QWidget*>(thisObject());
}


I create the binding with QtScript engine:


WidgetPrototype WidgetProtoObject();

QScriptValue widgetProto = engine.newQObject(&WidgetProtoObject);
engine.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);
QScriptValue widgetCtor = engine.newFunction(WidgetProtoObject.construct, widgetProto);
engine.globalObject().setProperty("Widget", widgetCtor);




var tem = new Widget();
tem.whatsThis="asdcvbs";
print(tem.whatsThis); // the script engine prints out "asdcvbs", but no from my whatsThis function in WidgetPrototype
print(tem.isActiveWindow); // I didnt add a slot for this property, but I get no mistake


Does anybody know where the mistake is?:confused:

seneca
17th February 2009, 09:25
One difference to my working prototypes is the constructor. Translated to your class it would be:


QScriptValue WidgetPrototype::construct(QScriptContext *ctx, QScriptEngine *eng)
{
if(ctx->argumentCount()==0)
{
// use newQObject; script ownership
return eng->newQObject(new QWidget(), QScriptEngine::ScriptOwnership);
}

return eng->undefinedValue();
}

Other differences:


// prototype object not on stack:
WidgetPrototype* WidgetPrototypeObject = new WidgetPrototype();

// make it script owned:
QScriptValue widgetProto = engine.newQObject(WidgetPrototypeObject, QScriptEngine::ScriptOwnership);

engine.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);
QScriptValue widgetCtor = engine.newFunction(WidgetPrototype::construct, widgetProto);
engine.globalObject().setProperty("Widget", widgetCtor);

zack
17th February 2009, 10:00
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()..

seneca
17th February 2009, 10:38
I didn't test your class, but mine work as expected.

Wild guess, would this maybe make a difference:


var tem = new Widget;