QScriptable类

QScriptable类
The QScriptable class provides access to the Qt Script environment from Qt C++ member functions.
QScriptable类提供从Qt C++成员函数访问Qt Script环境。
With QScriptEngine::newQObject(), you can expose the signals and slots and properties of any QObject (or subclass) to script code. QScriptable augments this functionality by giving your C++ members access to the Qt Script environment they are invoked in; conceptually, it is similar to QObject::sender().
用newQObject(),你可以暴露信号,槽和任何QObject的属性到脚本代码。通过给你的C++成员函数增加QScriptable参数功能来访问它们被调用的Qt Script环境;从概念上讲,它类似于QObject::sender()。
By subclassing QScriptable, you get the following functions in your class: thisObject(), argumentCount(), argument(), context() and engine(). With these functions, you have full access to the Qt Script environment from the slots and property access functions of your class, when they are invoked from script code.
通过子类化QScriptable,你可以在你的类中得到下面的函数:thisObject(),argumentCount(),argument(),context()和engine().当从脚本代码中调用它们,用这些函数,你就可以从你的类的槽和属性访问函数中完全访问Qt Script的环境。
For example, you can throw a Qt Script exception from a slot; manipulate the `this’ object associated with the function call; inspect the arguments stored in the QScriptContext to know the “real” arguments passed to the function from script code; and call script functions from your slot.
例如,你可以从一个槽中抛出一个Qt Script异常;操作跟调用函数关联的this对象;检查存储在QScriptContext中的参数了解从脚本中传递给函数的真实参数。并且从你的槽中调用脚本函数。
A typical use case of QScriptable is to implement prototype objects for custom C++ types. You define the scriptable interface of your custom type in a QScriptable subclass using properties and slots; then you wrap an instance of your class using QScriptEngine::newQObject(), and finally pass the result to QScriptEngine::setDefaultPrototype(). See the Default Prototypes Example to see how this can be done.
QScriptable的一个典型的用例是用来实现自定义C++类型的原型对象。你定义你的自定义类型的脚本化接口在一个QScriptable子类中使用属性和槽;然后你使用newQObject()封装你的类的实例。并且最后传递一个结果到setDefaultPrototype()。
The following is what subclassing QScriptable typically looks like:
下面是典型的子类化QScriptable的例子。
class MyScriptableObject: public QObject,
protected QScriptable
{
Q_OBJECT

public slots:
void doSomething();
double doSomethingElse();
}
The only difference from regular QObject subclassing is that you also inherit from QScriptable.
不同于常规子类化QObject的是你也必须继承QScriptable.
In the implementation of your slots, you can then use the functions inherited from QScriptable:
在你的槽的实现中,你可以使用继承自QScriptable的函数。
void MyScriptableObject::doSomething()
{
context()->throwError(“Threw an error from a slot”);
}

double MyScriptableObject::doSomethingElse()
{
return qscriptvalue_cast(thisObject());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值