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