qt scritable object
来源:互联网 发布:淘宝外卖和饿了么合并 编辑:程序博客网 时间:2024/06/05 23:07
分类: WINDOWS
QScriptValue myQObjectConstructor(QScriptContext *context, QScriptEngine *engine)
{
// let the engine manage the new object's lifetime.
return
engine->newQObject(
new
MyQObject(), QScriptEngine::ScriptOwnership);
Default Conversion from Qt Script to C++
The following table describes the default conversion from a QScriptValue to a C++ type.
bool
QScriptValue::toBool()
int
QScriptValue::toInt32()
uint
QScriptValue::toUInt32()
float
float(QScriptValue::toNumber())
double
QScriptValue::toNumber()
short
short(QScriptValue::toInt32())
ushort
QScriptValue::toUInt16()
char
char(QScriptValue::toInt32())
uchar
unsigned char(QScriptValue::toInt32())
qlonglong
qlonglong(QScriptValue::toInteger())
qulonglong
qulonglong(QScriptValue::toInteger())
QString
An empty string if the QScriptValue is null or undefined; QScriptValue::toString() otherwise.
QDateTime
QScriptValue::toDateTime()
QDate
QScriptValue::toDateTime().date()
QRegExp
QScriptValue::toRegExp()
QObject*
QScriptValue::toQObject()
QWidget*
QScriptValue::toQObject()
QVariant
QScriptValue::toVariant()
QChar
If the QScriptValue is a string, the result is the first character of the string, or a nullQChar if the string is empty; otherwise, the result is a QChar constructed from the unicode obtained by converting the QScriptValue to a ushort.
QStringList
If the QScriptValue is an array, the result is a QStringList constructed from the result of QScriptValue::toString() for each array element; otherwise, the result is an emptyQStringList.
QVariantList
If the QScriptValue is an array, the result is a QVariantList constructed from the result of QScriptValue::toVariant() for each array element; otherwise, the result is an emptyQVariantList.
QVariantMap
If the QScriptValue is an object, the result is a QVariantMap with a (key, value) pair of the form (propertyName, propertyValue.toVariant()) for each property, usingQScriptValueIterator to iterate over the object's properties.
QObjectList
If the QScriptValue is an array, the result is a QObjectList constructed from the result of QScriptValue::toQObject() for each array element; otherwise, the result is an emptyQObjectList.
QList<int>
If the QScriptValue is an array, the result is a QList<int> constructed from the result of QScriptValue::toInt32() for each array element; otherwise, the result is an emptyQList<int>.
Default Conversion from C++ to Qt Script
The following table describes the default behavior when a QScriptValue is constructed from a C++ type:
void
QScriptEngine::undefinedValue()
bool
QScriptValue(engine, value)
int
QScriptValue(engine, value)
uint
QScriptValue(engine, value)
float
QScriptValue(engine, value)
double
QScriptValue(engine, value)
short
QScriptValue(engine, value)
ushort
QScriptValue(engine, value)
char
QScriptValue(engine, value)
uchar
QScriptValue(engine, value)
QString
QScriptValue(engine, value)
qlonglong
QScriptValue(engine, qsreal(value)). Note that the conversion may lead to loss of precision, since not all 64-bit integers can be represented using the qsreal type.
qulonglong
QScriptValue(engine, qsreal(value)). Note that the conversion may lead to loss of precision, since not all 64-bit unsigned integers can be represented using the qsreal type.
QChar
QScriptValue(this, value.unicode())
QDateTime
QScriptEngine::newDate(value)
QDate
QScriptEngine::newDate(value)
QRegExp
QScriptEngine::newRegExp(value)
QObject*
QScriptEngine::newQObject(value)
QWidget*
QScriptEngine::newQObject(value)
QVariant
QScriptEngine::newVariant(value)
QStringList
A new script array (created with QScriptEngine::newArray()), whose elements are created using the QScriptValue(QScriptEngine *, QString) constructor for each element of the list.
QVariantList
A new script array (created with QScriptEngine::newArray()), whose elements are created using QScriptEngine::newVariant() for each element of the list.
QVariantMap
A new script object (created with QScriptEngine::newObject()), whose properties are initialized according to the (key, value) pairs of the map.
QObjectList
A new script array (created with QScriptEngine::newArray()), whose elements are created using QScriptEngine::newQObject() for each element of the list.
QList<int>
A new script array (created with QScriptEngine::newArray()), whose elements are created using the QScriptValue(QScriptEngine *, int) constructor for each element of the list.
The following script defines a Qt Script object that has a toKelvin() function:
({ unitName:
"Celsius"
,
toKelvin: function(x) {
return
x + 273; }
})
obtained and called from C++:
QScriptValue object = engine.evaluate(
"({ unitName: 'Celsius', toKelvin: function(x) { return x + 273; } })"
);
QScriptValue toKelvin = object.property(
"toKelvin"
);
QScriptValue result = toKelvin.call(object, QScriptValueList() << 100);
qDebug() << result.toNumber();
// 373
global function add():
function add(a, b) {
return
a + b;
}
C++ code might call the add() function as follows:
QScriptValue add = engine.globalObject().property(
"add"
);
qDebug() << add.call(QScriptValue(), QScriptValueList() << 1 << 2).toNumber();
// 3
var getProperty = function(name) {
return
this
[name]; };
name =
"Global Object"
;
// creates a global variable
print(getProperty(
"name"
));
// "Global Object"
var myObject = { name:
'My Object'
};
print(getProperty.call(myObject,
"name"
));
// "My Object"
myObject.getProperty = getProperty;
print(myObject.getProperty(
"name"
));
// "My Object"
getProperty.name =
"The getProperty() function"
;
getProperty.getProperty = getProperty;
getProperty.getProperty(
"name"
);
// "The getProperty() function"
var o = { a: 1, b: 2, sum: function() {
return
a + b; } };
print(o.sum());
// reference error, or sum of global variables a and b!!
QScriptValue getProperty(QScriptContext *ctx, QScriptEngine *eng)
{
QString name = ctx->argument(0).toString();
return
ctx->thisObject().property(name);
}
Call QScriptEngine::newFunction() to wrap the function.
这样就可以创建一个wrapper 内部hold一个c++的函数,而可以被js使用
Making a C++ object available to Scripts Written in QtScript
class
MyObject :
public
QObject
{
Q_OBJECT
public
:
MyObject( ... );
void
aNonScriptableFunction();
public
slots:
// these functions (slots) will be available in QtScript
void
calculate( ... );
void
setEnabled(
bool
enabled );
bool
isEnabled()
const
;
private
:
....
};
class
MyObject :
public
QObject
{
Q_OBJECT
public
:
Q_INVOKABLE
void
thisMethodIsInvokableInQtScript();
void
thisMethodIsNotInvokableInQtScript();
...
};
var obj =
new
MyObject;
obj.enabled =
true
;
print(
"obj is enabled: "
+ obj.enabled );
class
MyObject :
public
QObject
{
Q_OBJECT
// define the enabled property
Q_PROPERTY(
bool
enabled WRITE setEnabled READ isEnabled )
Q_PROPERTY(
int
nonScriptableProperty READ foo WRITE bar SCRIPTABLE
false
)
class
MyObject :
public
QObject
{
Q_OBJECT
// define the enabled property
Q_PROPERTY(
bool
enabled WRITE setEnabled READ isEnabled )
public
:
MyObject( ... );
void
aNonScriptableFunction();
public
slots:
// these functions (slots) will be available in QtScript
void
calculate( ... );
void
setEnabled(
bool
enabled );
bool
isEnabled()
const
;
signals:
// the signals
void
enabledChanged(
bool
newState );
private
:
....
};
function enabledChangedHandler( b )
{
print(
"state changed to: "
+ b );
}
function init()
{
var obj =
new
MyObject();
// connect a script function to the signal
obj[
"enabledChanged(bool)"
].connect(enabledChangedHandler);
obj.enabled =
true
;
print(
"obj is enabled: "
+ obj.enabled );
}
QScriptContext
- Get the arguments that were passed to the function.
- Get the this object.
- Find out whether the function was called with the new operator (the significance of this will be explained later).
- Throw a script error.
- Get the function object that's being invoked.
- Get the activation object (the object used to hold local variables).
function add(a, b) {
return
a + b;
}
function add() {
if
(arguments.length != 2)
throw
Error(
"add() takes exactly two arguments"
);
if
(typeof arguments[0] !=
"number"
)
throw
TypeError(
"add(): first argument is not a number"
);
if
(typeof arguments[1] !=
"number"
)
throw
TypeError(
"add(): second argument is not a number"
);
return
arguments[0] + arguments[1];
}
function add() {
return
arguments[0] + arguments[1];
}
function add() {
if
(arguments.length != 2)
throw
Error(
"add() takes exactly two arguments"
);
return
arguments[0] + arguments[1];
}
QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
{
double
a = ctx->argument(0).toNumber();
double
b = ctx->argument(1).toNumber();
return
a + b;
}
QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
{
if
(ctx->argumentCount() != 2)
return
ctx->throwError(
"add() takes exactly two arguments"
);
double
a = ctx->argument(0).toNumber();
double
b = ctx->argument(1).toNumber();
return
a + b;
}
QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
{
if
(ctx->argumentCount() != 2)
return
ctx->throwError(
"add() takes exactly two arguments"
);
if
(!ctx->argument(0).isNumber())
return
ctx->throwError(QScriptContext::TypeError,
"add(): first argument is not a number"
);
if
(!ctx->argument(1).isNumber())
return
ctx->throwError(QScriptContext::TypeError,
"add(): second argument is not a number"
);
double
a = ctx->argument(0).toNumber();
double
b = ctx->argument(1).toNumber();
return
a + b;
}
- qt scritable object
- Qt Object Model
- Qt Object 类简介
- qt meta-object system
- 初识Qt Object 类
- Qt Meta Object
- Qt Meta-Object System
- Qt-Meat-Object
- QT: Meta-Object 系统
- Qt Meta-Object System
- Qt--Meta-Object Model
- QT的 Meta-Object系统
- Using ActiveX Object in Qt
- QT的Meta-Object系统
- Qt之Meta-Object系统
- Inside QT Series (八):Meta Object Class
- Qt 如何实现的 Meta Object
- Qt 元对象系统(Meta-Object System)
- 51单片机的内存优化
- c语言strcmp实现
- 使用js和正则表达式验证邮箱、手机号码、密码等
- 通过dsh批量管理Linux服务器
- codeforce Dima and Guards
- qt scritable object
- Java语言实验机器与环境及JDK开发工具简介及Java程序开发步骤
- Javascript事件总结
- hdu-2042-不容易系列之二
- PostgreSQL的时间函数
- 让目录来整理你的博客
- 51单片机基础剖析(基于C语言)
- 第十节初步探险
- 大小之差 (2013年11月24日 蓝桥杯校内选拔赛c\c++B组)