本文整理汇总了C++中QScriptEngine::newQObject方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptEngine::newQObject方法的具体用法?C++ QScriptEngine::newQObject怎么用?C++ QScriptEngine::newQObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QScriptEngine
的用法示例。
在下文中一共展示了QScriptEngine::newQObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QScriptEngine engine;
QFile scriptFile(":/object.js");
scriptFile.open(QFile::ReadOnly);
engine.evaluate(scriptFile.readAll());
scriptFile.close();
QTextEdit editor;
QTimer timer;
QScriptValue constructor = engine.evaluate("Object");
QScriptValueList arguments;
arguments << engine.newQObject(&timer);
arguments << engine.newQObject(&editor);
QScriptValue object = constructor.construct(arguments);
if (engine.hasUncaughtException()) {
qDebug() << engine.uncaughtException().toString();
}
editor.show();
timer.start(1000);
return app.exec();
}
示例2: main
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(tetrix);
//! [1]
QApplication app(argc, argv);
QScriptEngine engine;
QScriptValue Qt = engine.newQMetaObject(QtMetaObject::get());
Qt.setProperty("App", engine.newQObject(&app));
engine.globalObject().setProperty("Qt", Qt);
//! [1]
#ifndef QT_NO_SCRIPTTOOLS
QScriptEngineDebugger debugger;
debugger.attachTo(&engine);
QMainWindow *debugWindow = debugger.standardWindow();
debugWindow->resize(1024, 640);
#endif
//! [2]
evaluateFile(engine, ":/tetrixpiece.js");
evaluateFile(engine, ":/tetrixboard.js");
evaluateFile(engine, ":/tetrixwindow.js");
//! [2]
//! [3]
TetrixUiLoader loader;
QFile uiFile(":/tetrixwindow.ui");
uiFile.open(QIODevice::ReadOnly);
QWidget *ui = loader.load(&uiFile);
uiFile.close();
QScriptValue ctor = engine.evaluate("TetrixWindow");
QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership);
QScriptValue tetrix = ctor.construct(QScriptValueList() << scriptUi);
//! [3]
QPushButton *debugButton = qFindChild<QPushButton*>(ui, "debugButton");
#ifndef QT_NO_SCRIPTTOOLS
QObject::connect(debugButton, SIGNAL(clicked()),
debugger.action(QScriptEngineDebugger::InterruptAction),
SIGNAL(triggered()));
QObject::connect(debugButton, SIGNAL(clicked()),
debugWindow, SLOT(show()));
#else
debugButton->hide();
#endif
//! [4]
ui->resize(550, 370);
ui->show();
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
return app.exec();
//! [4]
}
示例3: QScriptEngine
QScriptEngine * ScriptEngineWorker::createScriptEngine(bool supportThreads)
{
QScriptEngine *engine = new QScriptEngine();
QLOG_INFO() << "New script engine" << engine << ", thread:" << QThread::currentThread();
Scriptable<BatteryInterface>::registerMetatype(engine);
Scriptable<ColorSensorInterface>::registerMetatype(engine);
Scriptable<DisplayInterface>::registerMetatype(engine);
Scriptable<EncoderInterface>::registerMetatype(engine);
Scriptable<EventCodeInterface>::registerMetatype(engine);
Scriptable<EventDeviceInterface>::registerMetatype(engine);
Scriptable<EventInterface>::registerMetatype(engine);
Scriptable<GamepadInterface>::registerMetatype(engine);
Scriptable<GyroSensorInterface>::registerMetatype(engine);
Scriptable<FifoInterface>::registerMetatype(engine);
Scriptable<KeysInterface>::registerMetatype(engine);
Scriptable<LedInterface>::registerMetatype(engine);
Scriptable<LineSensorInterface>::registerMetatype(engine);
Scriptable<MailboxInterface>::registerMetatype(engine);
Scriptable<MotorInterface>::registerMetatype(engine);
Scriptable<ObjectSensorInterface>::registerMetatype(engine);
Scriptable<SensorInterface>::registerMetatype(engine);
Scriptable<SoundSensorInterface>::registerMetatype(engine);
Scriptable<QTimer>::registerMetatype(engine);
qScriptRegisterMetaType(engine, timeValToScriptValue, timeValFromScriptValue);
Scriptable<VectorSensorInterface>::registerMetatype(engine);
qScriptRegisterSequenceMetaType<QVector<int>>(engine);
qScriptRegisterSequenceMetaType<QStringList>(engine);
engine->globalObject().setProperty("brick", engine->newQObject(&mBrick));
engine->globalObject().setProperty("script", engine->newQObject(&mScriptControl));
if (mMailbox) {
engine->globalObject().setProperty("mailbox", engine->newQObject(mMailbox));
}
// Gamepad can still be accessed from script as brick.gamepad(), 'gamepad' variable is here for backwards
// compatibility.
if (mBrick.gamepad()) {
engine->globalObject().setProperty("gamepad", engine->newQObject(mBrick.gamepad()));
}
if (supportThreads) {
engine->globalObject().setProperty("Threading", engine->newQObject(&mThreading));
}
for (const auto &step : mCustomInitSteps) {
step(engine);
}
evalSystemJs(engine);
engine->setProcessEventsInterval(1);
return engine;
}
示例4:
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);
}
示例5: main
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
QDeclarativeContext *context = view.rootContext();
context->setContextProperty("backgroundColor",
QColor(Qt::yellow));
KDeclarative kdeclarative;
kdeclarative.setDeclarativeEngine(view.engine());
kdeclarative.initialize();
//binds things like kconfig and icons
kdeclarative.setupBindings();
//If all gone well, the QScriptEngine has been extracted
QScriptEngine *scriptEngine = kdeclarative.scriptEngine();
Q_ASSERT(scriptEngine);
//Bind a test QObject in the "QtScript way"
QScriptValue global = scriptEngine->globalObject();
TestObject *testObject = new TestObject();
QScriptValue testValue = scriptEngine->newQObject(testObject);
testValue.setScope(global);
global.setProperty("testObject", testValue);
view.setSource(QUrl::fromLocalFile("test.qml"));
view.show();
return app.exec();
}
示例6: addObject
void addObject(QObject* object, const QString& name = QString()) {
Q_ASSERT( m_engine );
Q_ASSERT( ! m_engine->hasUncaughtException() );
QScriptValue global = m_engine->globalObject();
QScriptValue value = m_engine->newQObject(object, QScriptEngine::AutoOwnership);
global.setProperty(name.isEmpty() ? object->objectName() : name, value);
}
示例7: qobjectAsActivationObject
void tst_QScriptContext::qobjectAsActivationObject()
{
QScriptEngine eng;
QObject object;
QScriptValue scriptObject = eng.newQObject(&object);
QScriptContext *ctx = eng.pushContext();
ctx->setActivationObject(scriptObject);
QVERIFY(ctx->activationObject().equals(scriptObject));
QVERIFY(!scriptObject.property("foo").isValid());
eng.evaluate("function foo() { return 123; }");
{
QScriptValue val = scriptObject.property("foo");
QVERIFY(val.isValid());
QVERIFY(val.isFunction());
}
QVERIFY(!eng.globalObject().property("foo").isValid());
QVERIFY(!scriptObject.property("bar").isValid());
eng.evaluate("var bar = 123");
{
QScriptValue val = scriptObject.property("bar");
QVERIFY(val.isValid());
QVERIFY(val.isNumber());
QCOMPARE(val.toInt32(), 123);
}
QVERIFY(!eng.globalObject().property("bar").isValid());
{
QScriptValue val = eng.evaluate("delete foo");
QVERIFY(val.isBool());
QVERIFY(val.toBool());
QVERIFY(!scriptObject.property("foo").isValid());
}
}
示例8: World
World *ScriptEvaluator::evaluate(const QString &script)
{
QScriptEngine engine;
World* world = new World();
QScriptValue worldValue = engine.newQObject(world);
engine.globalObject().setProperty("world", worldValue);
QScriptValue directionalLightClass = engine.scriptValueFromQMetaObject<DirectionalLight>();
engine.globalObject().setProperty("DirectionalLight", directionalLightClass);
engine.evaluate(script);
if (engine.hasUncaughtException())
{
QMessageBox messageBox;
messageBox.setIcon(QMessageBox::Warning);
messageBox.setWindowTitle("Script error");
messageBox.setText("An error occurred while executing the script.");
messageBox.setInformativeText(engine.uncaughtException().toString());
messageBox.setDetailedText(engine.uncaughtExceptionBacktrace().join("\n"));
messageBox.exec();
return 0;
}
return world;
}
示例9: main
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MagicCore SomeMagicCoreClass;
QScriptEngine scriptEngine;
QScriptValue scriptMagicCore = scriptEngine.newQObject (&SomeMagicCoreClass);
Q_ASSERT (scriptMagicCore.isQObject());
scriptEngine.globalObject().setProperty("Core", scriptMagicCore);
ReadScriptsFromPath(QDir("./scripts/objects/"), scriptEngine);
ReadScriptsFromPath(QDir("./scripts/levels/"), scriptEngine);
char szLine[256];
strcpy(szLine, "");
std::cout << std::endl << "Entering scripting console: " << std::endl;
while (strcmp(szLine, "exit") != 0)
{
// evaluate script line and store return value
QScriptValue retVal = scriptEngine.evaluate(szLine);
if (retVal.isUndefined() == false)
std::cout << "ScriptEngine returns: " << retVal.toString().toStdString() << std::endl;
// read line
strcpy(szLine, "");
std::cin.getline(szLine, 256);
}
/* QScriptValue object = scriptEngine.globalObject().property("testObject");
Q_ASSERT(object.isValid());
QScriptValue SlotMethod = object.property("ExampleSlot1");
Q_ASSERT(SlotMethod.isFunction());
bool bOk = qScriptConnect(&SomeMagicCoreClass, SIGNAL(TestSignal()), object, SlotMethod);
Q_ASSERT(bOk);
QScriptValue object2 = scriptEngine.globalObject().property("testObject2");
Q_ASSERT(object2.isValid());
QScriptValue SlotMethod2 = object2.property("ExampleSlot2");
Q_ASSERT(SlotMethod2.isFunction());
bool bOk2 = qScriptConnect(&SomeMagicCoreClass, SIGNAL(TestSignal()), object2, SlotMethod2);
Q_ASSERT(bOk2);
SomeMagicCoreClass.TriggerSignal();*/
return 0;
/*a.exit(0);
return a.exec();*/
}
示例10: main
int main(int argc, char **argv)
{
Q_INIT_RESOURCE(rtfedit);
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QScriptEngine engine;
QFile scriptFile(":/rtfedit.js");
scriptFile.open(QIODevice::ReadOnly);
engine.evaluate(QObject::tr(scriptFile.readAll()));
scriptFile.close();
RTFUiLoader loader;
QFile uiFile(":/rtfedit.ui");
uiFile.open(QIODevice::ReadOnly);
QWidget *ui = loader.load(&uiFile);
uiFile.close();
QScriptValue func = engine.evaluate("RTF");
QScriptValue scriptUi = engine.newQObject(ui);
QScriptValue table = func.construct(QScriptValueList() << scriptUi);
if(engine.hasUncaughtException()) {
QScriptValue value = engine.uncaughtException();
QString lineNumber = QString("\nLine Number:%1\n").arg(engine.uncaughtExceptionLineNumber());
QStringList btList = engine.uncaughtExceptionBacktrace();
QString trace;
for(short i=0; i<btList.size(); ++i)
trace += btList.at(i);
QMessageBox::information(NULL, QObject::tr("Exception"), value.toString() + lineNumber + trace );
}
ui->show();
return app.exec();
}
示例11: defineFunction
inline void defineFunction(ScriptEngine *se, const QString &name,
QScriptEngine::FunctionSignature f) {
QScriptEngine *engine = se->getQScriptEngine();
QScriptValue fObj = engine->newFunction(f);
fObj.setData(engine->newQObject(se));
engine->globalObject().setProperty(name, fObj);
}
示例12: main
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString fileName = "load.qs";
QFile scriptFile(fileName);
if (!scriptFile.open(QIODevice::ReadOnly))
{
// handle error
qDebug()<<"File open error";
}
else
{
QFileInfo info(scriptFile);
qDebug()<< "File Path" + info.absoluteFilePath();
}
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();
ScriptFunctions functions;
QScriptEngine myEngine;
QScriptValue scriptFunctions = myEngine.newQObject(&functions);
myEngine.globalObject().setProperty("nativeFunctions", scriptFunctions);
myEngine.globalObject().setProperty("load", myEngine.newFunction(loadScripts, /*length=*/1));
QScriptValue result = myEngine.evaluate(contents, fileName);
if (myEngine.hasUncaughtException()) {
int line = myEngine.uncaughtExceptionLineNumber();
qDebug() << "uncaught exception at line" << line << ":" << result.toString();
}
return a.exec();
}
示例13: init
bool init() {
if( m_script->action()->hadError() )
m_script->action()->clearError();
delete m_engine;
m_engine = new QScriptEngine();
// load the Qross QScriptExtensionPlugin plugin that provides
// us a bridge between Qross and QtScript. See here plugin.h
m_engine->importExtension("qross");
if( m_engine->hasUncaughtException() ) {
handleException();
delete m_engine;
m_engine = 0;
return false;
}
// the Qross QScriptExtensionPlugin exports the "Qross" property.
QScriptValue global = m_engine->globalObject();
m_qross = global.property("Qross");
Q_ASSERT( m_qross.isQObject() );
Q_ASSERT( ! m_engine->hasUncaughtException() );
// Attach our Qross::Action instance to be able to access it in
// scripts. Just like at the Kjs-backend we publish our own
// action as "self".
m_self = m_engine->newQObject( m_script->action() );
global.setProperty("self", m_self, QScriptValue::ReadOnly|QScriptValue::Undeletable);
{ // publish the global objects.
QHash< QString, QObject* > objects = Manager::self().objects();
QHash< QString, QObject* >::Iterator it(objects.begin()), end(objects.end());
for(; it != end; ++it)
global.setProperty(it.key(), m_engine->newQObject( it.value() ) );
}
{ // publish the local objects.
QHash< QString, QObject* > objects = m_script->action()->objects();
QHash< QString, QObject* >::Iterator it(objects.begin()), end(objects.end());
for(; it != end; ++it) {
copyEnumsToProperties( it.value() );
global.setProperty(it.key(), m_engine->newQObject( it.value() ) );
}
}
return ! m_engine->hasUncaughtException();
}
示例14: main
// Perhaps shpw entire example for getting debugger up with script
int main(int argv, char **args)
{
QApplication app(argv, args);
QString fileName("helloscript.qs");
QFile scriptFile(fileName);
scriptFile.open(QIODevice::ReadOnly);
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();
QScriptEngine *engine = new QScriptEngine();
QScriptEngineDebugger *debugger = new QScriptEngineDebugger();
debugger->attachTo(engine);
// Set up configuration with only stack and code
QWidget *widget = new QWidget;
//![0]
QWidget *codeWindow = debugger->widget(QScriptEngineDebugger::CodeWidget);
QWidget *stackWidget = debugger->widget(QScriptEngineDebugger::StackWidget);
QLayout *layout = new QHBoxLayout;
layout->addWidget(codeWindow);
layout->addWidget(stackWidget);
//![0]
//![1]
QAction *continueAction = debugger->action(QScriptEngineDebugger::ContinueAction);
QAction *stepOverAction = debugger->action(QScriptEngineDebugger::StepOverAction);
QAction *stepIntoAction = debugger->action(QScriptEngineDebugger::StepIntoAction);
QToolBar *toolBar = new QToolBar;
toolBar->addAction(continueAction);
//![1]
toolBar->addAction(stepOverAction);
toolBar->addAction(stepIntoAction);
layout->addWidget(toolBar);
continueAction->setIcon(QIcon("copy.png"));
debugger->setAutoShowStandardWindow(false);
widget->setLayout(layout);
widget->show();
QPushButton button;
QScriptValue scriptButton = engine->newQObject(&button);
engine->globalObject().setProperty("button", scriptButton);
//![2]
debugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
engine->evaluate(contents, fileName);
//![2]
return app.exec();
}
示例15: ConfigureScriptEngine
bool TCPNetworkServerPlugin::ConfigureScriptEngine(QScriptEngine &engine)
{
QScriptValue TCPNetworkServerProto = engine.newQObject(TCPNetworkServerObject);
engine.setDefaultPrototype(qMetaTypeId<TCPNetworkServer*>(), TCPNetworkServerProto);
QScriptValue TCPNetworkServerCtor = engine.newFunction(TCPNetworkServer::ctor__extensionname, TCPNetworkServerProto);
engine.globalObject().setProperty(PLUGIN_SCRIPTOBJECT_NAME, TCPNetworkServerCtor);
//int nRes =
qRegisterMetaType<TCPNetworkServer>(PLUGIN_SCRIPTOBJECT_NAME);
//FinalizeScriptObjectScriptContext(engine, TCPNetworkServerObject);
return true;
}