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);
void setWhatsThis
(const QString &text
) const;
void setToolTip
(const QString &text
) const;
static QScriptValue construct(QScriptContext *ctx, QScriptEngine *eng);
private:
};
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;
};
To copy to clipboard, switch view to plain text mode
WidgetPrototype
::WidgetPrototype(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());
}
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());
}
To copy to clipboard, switch view to plain text mode
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);
WidgetPrototype WidgetProtoObject();
QScriptValue widgetProto = engine.newQObject(&WidgetProtoObject);
engine.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);
QScriptValue widgetCtor = engine.newFunction(WidgetProtoObject.construct, widgetProto);
engine.globalObject().setProperty("Widget", widgetCtor);
To copy to clipboard, switch view to plain text mode
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
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
To copy to clipboard, switch view to plain text mode
Does anybody know where the mistake is?
Bookmarks