分类: WINDOWS
1
2
3
4
|
QScriptValue myQObjectConstructor(QScriptContext *context, QScriptEngine *engine)
{
// let the engine manage the new object's lifetime.
return
engine->newQObject(
new
MyQObject(), QScriptEngine::ScriptOwnership);
|
The following table describes the default conversion from a QScriptValue to a C++ type.
C++ Type | Default Conversion |
---|---|
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>. |
The following table describes the default behavior when a QScriptValue is constructed from a C++ type:
C++ Type | Default Construction |
---|---|
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:
1
2
3
|
({ unitName:
"Celsius"
,
toKelvin: function(x) {
return
x + 273; }
})
|
obtained and called from C++:
1
2
3
4
|
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():
1
2
3
|
function add(a, b) {
return
a + b;
}
|
C++ code might call the add() function as follows:
1
2
|
QScriptValue add = engine.globalObject().property(
"add"
);
qDebug() << add.call(QScriptValue(), QScriptValueList() << 1 << 2).toNumber();
// 3
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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"
|
1
2
|
var o = { a: 1, b: 2, sum: function() {
return
a + b; } };
print(o.sum());
// reference error, or sum of global variables a and b!!
|
1
2
3
4
5
|
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使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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
:
....
};
|
1
2
3
4
5
6
7
8
9
10
|
class
MyObject :
public
QObject
{
Q_OBJECT
public
:
Q_INVOKABLE
void
thisMethodIsInvokableInQtScript();
void
thisMethodIsNotInvokableInQtScript();
...
};
|
1
2
3
|
var obj =
new
MyObject;
obj.enabled =
true
;
print(
"obj is enabled: "
+ obj.enabled );
|
1
2
3
4
5
|
class
MyObject :
public
QObject
{
Q_OBJECT
// define the enabled property
Q_PROPERTY(
bool
enabled WRITE setEnabled READ isEnabled )
|
1
|
Q_PROPERTY(
int
nonScriptableProperty READ foo WRITE bar SCRIPTABLE
false
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
:
....
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 );
}
|
1
2
3
|
function add(a, b) {
return
a + b;
}
|
1
2
3
4
5
6
7
8
9
|
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];
}
|
1
2
3
|
function add() {
return
arguments[0] + arguments[1];
}
|
1
2
3
4
5
|
function add() {
if
(arguments.length != 2)
throw
Error(
"add() takes exactly two arguments"
);
return
arguments[0] + arguments[1];
}
|
1
2
3
4
5
6
|
QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
{
double
a = ctx->argument(0).toNumber();
double
b = ctx->argument(1).toNumber();
return
a + b;
}
|
1
2
3
4
5
6
7
8
|
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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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;
}
|