本文整理汇总了C++中QScriptEngine::newArray方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptEngine::newArray方法的具体用法?C++ QScriptEngine::newArray怎么用?C++ QScriptEngine::newArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QScriptEngine
的用法示例。
在下文中一共展示了QScriptEngine::newArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getAuthServices
NResponse & NTcpServerSocketMusicServices::getYear(const NClientSession & session,
NResponse & response)
{
bool ok;
QString search = session.url().queryItemValue("search");
QStringList searches = search.split("+", QString::SkipEmptyParts);
searches = NConvert_n::fromUTF8PercentEncoding(searches);
int start = session.url().queryItemValue("start").toInt();
int limit = session.url().queryItemValue("limit").toInt(&ok);
if (!ok)
limit = 25;
QString dir = session.url().queryItemValue("dir");
const NTcpServerAuthSession authSession = getAuthServices().getSession(session.sessionId());
logMessage(session.socket()->peerAddress().toString(),
QString("%1 is looking for year: \"%2\"; start: %3; limit: %4, dir:\"%5\"").
arg(authSession.login()).arg(NConvert_n::fromUTF8PercentEncoding(search)).arg(start).arg(limit).arg(dir));
int totalCount = NMDB.getYearListCount(searches);
QScriptEngine se;
QScriptValue svRoot = se.newObject();
QScriptValue svData = se.newArray(totalCount);
svRoot.setProperty(RSP_DATA, svData);
bool succeed = NMDB.getYearList(se, svData, totalCount, searches, start,
limit, dir);
setJsonRootReponse(svRoot, totalCount, succeed);
response.setData(NJson::serializeToQByteArray(svRoot));
return response;
}
示例2: QScriptValue
// Configuration
NResponse & NTcpServerSocketCfgServices::getSharedDir(const NClientSession &, NResponse & response)
{
QScriptEngine se;
QScriptValue svRoot = se.newObject();
NDirList sd = NConfig::instance().sharedDirectories();
int count = sd.count();
svRoot.setProperty(RSP_SUCCESS , QScriptValue(count > 0));
svRoot.setProperty(RSP_MSG, QScriptValue(count > 0 ? RSP_MSG_LOADED : RSP_MSG_NO_RESULTS));
svRoot.setProperty(RSP_COUNT, QScriptValue(count));
QScriptValue svData = se.newArray(count);
svRoot.setProperty(RSP_DATA, svData);
for(int i = 0; i < count; i++)
{
NDir dir = sd.at(i);
QScriptValue svDir = se.newObject();
svData.setProperty(i, svDir);
svDir.setProperty("id", i);
svDir.setProperty("path", dir.path());
svDir.setProperty("recursive", dir.recursive());
svDir.setProperty("shared", dir.shared());
svDir.setProperty("exists", dir.exists());
}
response.setData(NJson::serializeToQByteArray(svRoot));
return response;
}
示例3: NDir
NResponse & NTcpServerSocketCfgServices::postSharedDir(const NClientSession & session, NResponse & response)
{
//logDebug("NTcpServerSocketCfgServices::svcPostSharedDir", session.postData());
QScriptEngine se;
se.evaluate("var data = " + QString::fromUtf8(session.content()));
QScriptValue svReadData = se.globalObject().property("data").property("data");
NDir dir = NDir(svReadData.property("path").toString(),
svReadData.property("recursive").toBool(),
svReadData.property("shared").toBool());
int id = getConfig().addSharedDirectory(dir);
QScriptValue svRoot = se.newObject();
svRoot.setProperty(RSP_SUCCESS , QScriptValue(true));
svRoot.setProperty(RSP_MSG, QScriptValue(RSP_MSG_LOADED));
// we add new user to response
QScriptValue svData = se.newArray(1);
svRoot.setProperty(RSP_DATA, svData);
QScriptValue svDir = se.newObject();
svData.setProperty(0, svDir);
svDir.setProperty("id", id);
svDir.setProperty("path", dir.path());
svDir.setProperty("recursive", dir.recursive());
svDir.setProperty("shared", dir.shared());
svDir.setProperty("exists", dir.exists());
//logDebug("NJson::serialize(svRoot)", NJson::serialize(svRoot));
response.setData(NJson::serializeToQByteArray(svRoot));
return response;
}
示例4: variantListToScriptValue
QScriptValue variantListToScriptValue(QVariantList& variantList, QScriptEngine& scriptEngine) {
QScriptValue scriptValue = scriptEngine.newArray();
for (int i = 0; i < variantList.size(); i++) {
scriptValue.setProperty(i, variantToScriptValue(variantList[i], scriptEngine));
}
return scriptValue;
}
示例5: CreateValue
QScriptValue CreateValue(const QVariant& value, QScriptEngine& engine)
{
if(value.type() == QVariant::Map)
{
QScriptValue obj = engine.newObject();
QVariantMap map = value.toMap();
QVariantMap::const_iterator it = map.begin();
QVariantMap::const_iterator end = map.end();
while(it != end)
{
obj.setProperty( it.key(), ::CreateValue(it.value(), engine) );
++it;
}
return obj;
}
if(value.type() == QVariant::List)
{
QVariantList list = value.toList();
QScriptValue array = engine.newArray(list.length());
for(int i=0; i<list.count(); i++)
array.setProperty(i, ::CreateValue(list.at(i),engine));
return array;
}
switch(value.type())
{
case QVariant::String:
return QScriptValue(value.toString());
case QVariant::Int:
return QScriptValue(value.toInt());
case QVariant::UInt:
return QScriptValue(value.toUInt());
case QVariant::Bool:
return QScriptValue(value.toBool());
case QVariant::ByteArray:
return QScriptValue(QLatin1String(value.toByteArray()));
case QVariant::Double:
return QScriptValue((qsreal)value.toDouble());
default:
break;
}
if(value.isNull())
return QScriptValue(QScriptValue::NullValue);
return engine.newVariant(value);
}
示例6: iterateArrayAndRemoveAllElements
void tst_QScriptValueIterator::iterateArrayAndRemoveAllElements()
{
QScriptEngine engine;
QScriptValue array = engine.newArray();
for (int i = 0; i < 20000; ++i)
array.setProperty(i, i);
QBENCHMARK {
QScriptValueIterator it(array);
while (it.hasNext()) {
it.next();
it.remove();
}
}
}
示例7:
void QFormScriptRunner::QFormScriptRunnerPrivate::initializeEngine(QWidget *w, const WidgetList &children, QScriptEngine &scriptEngine) {
// Populate the script variables. This pushes a context which must be popped.
QScriptContext *ctx = scriptEngine.pushContext();
QScriptValue widgetObject = scriptEngine.newQObject(w);
QScriptValue childrenArray = scriptEngine.newArray (children.size());
for(int i = 0; i < children.size(); i++) {
childrenArray.setProperty(i, scriptEngine.newQObject(children[i]));
}
const QFormBuilderStrings &strings = QFormBuilderStrings::instance();
ctx ->activationObject().setProperty(strings.scriptWidgetVariable, widgetObject);
ctx ->activationObject().setProperty(strings.scriptChildWidgetsVariable, childrenArray);
}
示例8: iterateArrayAndDoubleElements
void tst_QScriptValueIterator::iterateArrayAndDoubleElements()
{
QScriptEngine engine;
QScriptValue array = engine.newArray();
for (int i = 0; i < 20000; ++i)
array.setProperty(i, i);
QBENCHMARK {
QScriptValueIterator it(array);
while (it.hasNext()) {
it.next();
it.setValue(QScriptValue(&engine, it.value().toNumber() * 2));
}
}
}
示例9: iterateArrayAndConvertNameToIndex
void tst_QScriptValueIterator::iterateArrayAndConvertNameToIndex()
{
QScriptEngine engine;
QScriptValue array = engine.newArray();
for (int i = 0; i < 20000; ++i)
array.setProperty(i, i);
QBENCHMARK {
QScriptValueIterator it(array);
while (it.hasNext()) {
it.next();
it.scriptName().toArrayIndex();
}
}
}
示例10: searchFinishedSlot
void GeoHelper::searchFinishedSlot(QGeoSearchReply *reply)
{
if (reply->error() == QGeoSearchReply::NoError)
{
QScriptEngine scriptEngine;
QScriptValue replyObject = scriptEngine.newArray();
QList<QGeoPlace> places = reply->places();
for (int i = 0; i < places.count(); i++)
{
QScriptValue placeObject = scriptEngine.newObject();
QScriptValue coordinateObject = scriptEngine.newObject();
QGeoCoordinate coordinate = places[i].coordinate();
coordinateObject.setProperty("latitude", QScriptValue(coordinate.latitude()));
coordinateObject.setProperty("longitude", QScriptValue(coordinate.longitude()));
placeObject.setProperty("coordinate", coordinateObject);
QScriptValue addressObject = scriptEngine.newObject();
QGeoAddress address = places[i].address();
if (!address.isEmpty())
{
addressObject.setProperty("country", address.country());
addressObject.setProperty("countryCode", address.countryCode());
addressObject.setProperty("state", address.state());
addressObject.setProperty("county", address.county());
addressObject.setProperty("city", address.city());
addressObject.setProperty("district", address.district());
addressObject.setProperty("street", address.street());
addressObject.setProperty("postcode", address.postcode());
}
placeObject.setProperty("address", addressObject);
replyObject.setProperty(i, placeObject);
}
QScriptValue fun = scriptEngine.evaluate("(function(a) { return JSON.stringify(a); })");
QScriptValueList args;
args << replyObject;
QScriptValue result = fun.call(QScriptValue(), args);
emit searchReply(result.toString());
}
}
示例11: toValue
QScriptDeclarativeClass::Value MetaCallArgument::toValue(QDeclarativeEngine *e)
{
QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(e);
if (type == qMetaTypeId<QScriptValue>()) {
return QScriptDeclarativeClass::Value(engine, *qscriptValuePtr);
} else if (type == QMetaType::Int) {
return QScriptDeclarativeClass::Value(engine, int(intValue));
} else if (type == QMetaType::UInt) {
return QScriptDeclarativeClass::Value(engine, uint(intValue));
} else if (type == QMetaType::Bool) {
return QScriptDeclarativeClass::Value(engine, boolValue);
} else if (type == QMetaType::Double) {
return QScriptDeclarativeClass::Value(engine, doubleValue);
} else if (type == QMetaType::Float) {
return QScriptDeclarativeClass::Value(engine, floatValue);
} else if (type == QMetaType::QString) {
return QScriptDeclarativeClass::Value(engine, *qstringPtr);
} else if (type == QMetaType::QObjectStar) {
if (qobjectPtr)
QDeclarativeData::get(qobjectPtr, true)->setImplicitDestructible();
QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(e);
return QScriptDeclarativeClass::Value(engine, priv->objectClass->newQObject(qobjectPtr));
} else if (type == qMetaTypeId<QList<QObject *> >()) {
QList<QObject *> &list = *qlistPtr;
QScriptValue rv = engine->newArray(list.count());
QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(e);
for (int ii = 0; ii < list.count(); ++ii) {
QObject *object = list.at(ii);
QDeclarativeData::get(object, true)->setImplicitDestructible();
rv.setProperty(ii, priv->objectClass->newQObject(object));
}
return QScriptDeclarativeClass::Value(engine, rv);
} else if (type == -1 || type == qMetaTypeId<QVariant>()) {
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(e);
QScriptValue rv = ep->scriptValueFromVariant(*qvariantPtr);
if (rv.isQObject()) {
QObject *object = rv.toQObject();
if (object)
QDeclarativeData::get(object, true)->setImplicitDestructible();
}
return QScriptDeclarativeClass::Value(engine, rv);
} else {
return QScriptDeclarativeClass::Value();
}
}
示例12: newInstance
void tst_QScriptClass::newInstance()
{
QScriptEngine eng;
TestClass cls(&eng);
QScriptValue obj1 = eng.newObject(&cls);
QVERIFY(!obj1.data().isValid());
QVERIFY(obj1.prototype().strictlyEquals(cls.prototype()));
QEXPECT_FAIL("", "classname is not implemented", Continue);
QCOMPARE(obj1.toString(), QString::fromLatin1("[object TestClass]"));
QCOMPARE(obj1.scriptClass(), (QScriptClass*)&cls);
QScriptValue num(&eng, 456);
QScriptValue obj2 = eng.newObject(&cls, num);
QVERIFY(obj2.data().strictlyEquals(num));
QVERIFY(obj2.prototype().strictlyEquals(cls.prototype()));
QCOMPARE(obj2.scriptClass(), (QScriptClass*)&cls);
QScriptValue obj3 = eng.newObject();
QCOMPARE(obj3.scriptClass(), (QScriptClass*)0);
obj3.setScriptClass(&cls);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls);
obj3.setScriptClass(0);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)0);
obj3.setScriptClass(&cls);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls);
TestClass cls2(&eng);
obj3.setScriptClass(&cls2);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls2);
// undefined behavior really, but shouldn't crash
QScriptValue arr = eng.newArray();
QVERIFY(arr.isArray());
QCOMPARE(arr.scriptClass(), (QScriptClass*)0);
QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setScriptClass() failed: cannot change class of non-QScriptObject");
arr.setScriptClass(&cls);
QEXPECT_FAIL("", "Changing class of arbitrary script object is not allowed (it's OK)", Continue);
QCOMPARE(arr.scriptClass(), (QScriptClass*)&cls);
QEXPECT_FAIL("", "Changing class of arbitrary script object is not allowed (it's OK)", Continue);
QVERIFY(!arr.isArray());
QVERIFY(arr.isObject());
}
示例13: newInstance
void tst_QScriptClass::newInstance()
{
QScriptEngine eng;
TestClass cls(&eng);
QScriptValue obj1 = eng.newObject(&cls);
QVERIFY(!obj1.data().isValid());
QVERIFY(obj1.prototype().strictlyEquals(cls.prototype()));
QCOMPARE(obj1.toString(), QString::fromLatin1("[object TestClass]"));
QCOMPARE(obj1.scriptClass(), (QScriptClass*)&cls);
QScriptValue num(&eng, 456);
QScriptValue obj2 = eng.newObject(&cls, num);
QVERIFY(obj2.data().strictlyEquals(num));
QVERIFY(obj2.prototype().strictlyEquals(cls.prototype()));
QCOMPARE(obj2.scriptClass(), (QScriptClass*)&cls);
QScriptValue obj3 = eng.newObject();
QCOMPARE(obj3.scriptClass(), (QScriptClass*)0);
obj3.setScriptClass(&cls);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls);
obj3.setScriptClass(0);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)0);
obj3.setScriptClass(&cls);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls);
TestClass cls2(&eng);
obj3.setScriptClass(&cls2);
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls2);
// undefined behavior really, but shouldn't crash
QScriptValue arr = eng.newArray();
QVERIFY(arr.isArray());
QCOMPARE(arr.scriptClass(), (QScriptClass*)0);
arr.setScriptClass(&cls);
QCOMPARE(arr.scriptClass(), (QScriptClass*)&cls);
QVERIFY(!arr.isArray());
QVERIFY(arr.isObject());
}
示例14: toValue
QScriptDeclarativeClass::Value MetaCallArgument::toValue(QDeclarativeEngine *e)
{
QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(e);
if (type == qMetaTypeId<QScriptValue>()) {
return QScriptDeclarativeClass::Value(engine, *((QScriptValue *)&data));
} else if (type == QMetaType::Int) {
return QScriptDeclarativeClass::Value(engine, *((int *)&data));
} else if (type == QMetaType::UInt) {
return QScriptDeclarativeClass::Value(engine, *((uint *)&data));
} else if (type == QMetaType::Bool) {
return QScriptDeclarativeClass::Value(engine, *((bool *)&data));
} else if (type == QMetaType::Double) {
return QScriptDeclarativeClass::Value(engine, *((double *)&data));
} else if (type == QMetaType::Float) {
return QScriptDeclarativeClass::Value(engine, *((float *)&data));
} else if (type == QMetaType::QString) {
return QScriptDeclarativeClass::Value(engine, *((QString *)&data));
} else if (type == QMetaType::QObjectStar) {
QObject *object = *((QObject **)&data);
QDeclarativeData::get(object, true)->setImplicitDestructible();
QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(e);
return QScriptDeclarativeClass::Value(engine, priv->objectClass->newQObject(object));
} else if (type == qMetaTypeId<QList<QObject *> >()) {
QList<QObject *> &list = *(QList<QObject *>*)&data;
QScriptValue rv = engine->newArray(list.count());
QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(e);
for (int ii = 0; ii < list.count(); ++ii) {
QObject *object = list.at(ii);
QDeclarativeData::get(object, true)->setImplicitDestructible();
rv.setProperty(ii, priv->objectClass->newQObject(object));
}
return QScriptDeclarativeClass::Value(engine, rv);
} else if (type == -1 || type == qMetaTypeId<QVariant>()) {
return QScriptDeclarativeClass::Value(engine, QDeclarativeEnginePrivate::get(e)->scriptValueFromVariant(*((QVariant *)&data)));
} else {
return QScriptDeclarativeClass::Value();
}
}
示例15: routingFinishedSlot
void GeoHelper::routingFinishedSlot(QGeoRouteReply * reply)
{
if (reply->error() == QGeoRouteReply::NoError)
{
QScriptEngine scriptEngine;
QScriptValue replyObject = scriptEngine.newArray();
QList<QGeoCoordinate> waypoints = reply->request().waypoints();
double lat1 = 0;
double lon1 = 0;
double lat2 = 0;
double lon2 = 0;
if (waypoints.count() > 0)
{
/*
QString msg = QString("lat %1, lon %2 => lat %3, lon %4").
arg(waypoints.at(0).latitude()).arg(waypoints.at(0).longitude()).
arg(waypoints.at((waypoints.count()-1)).latitude()).arg(waypoints.at((waypoints.count()-1)).longitude());
emit routingError(msg);
*/
lat1 = waypoints.at(0).latitude();
lon1 = waypoints.at(0).longitude();
lat2 = waypoints.at((waypoints.count()-1)).latitude();
lon2 = waypoints.at((waypoints.count()-1)).longitude();
}
for (int i = 0; i < reply->routes().size(); ++i)
{
QScriptValue routeObject = scriptEngine.newObject();
QGeoRoute route = reply->routes().at(i);
routeObject.setProperty("distance", QScriptValue(route.distance()));
routeObject.setProperty("travelTime", QScriptValue(route.travelTime()));
routeObject.setProperty("lat1", QScriptValue(lat1));
routeObject.setProperty("lon1", QScriptValue(lon1));
routeObject.setProperty("lat2", QScriptValue(lat2));
routeObject.setProperty("lon2", QScriptValue(lon2));
QScriptValue pathObject = scriptEngine.newArray();
QList<QGeoCoordinate> path = route.path();
for (int p = 0; p < path.length(); p++)
{
QScriptValue coordinateObject = scriptEngine.newObject();
coordinateObject.setProperty("latitude", QScriptValue(path[p].latitude()));
coordinateObject.setProperty("longitude", QScriptValue(path[p].longitude()));
pathObject.setProperty(p, coordinateObject);
}
routeObject.setProperty("path", pathObject);
replyObject.setProperty(i, routeObject);
}
QScriptValue fun = scriptEngine.evaluate("(function(a) { return JSON.stringify(a); })");
QScriptValueList args;
args << replyObject;
QScriptValue result = fun.call(QScriptValue(), args);
emit routingReply(result.toString());
}
}