Qt脚本功能包装 [英] Qt Script function wrapping

查看:50
本文介绍了Qt脚本功能包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Qt脚本的文档,发现如果误导文本,这将是完全混乱和充斥的.可以请一些简单的英文解释一下如何包装一个函数并在包装后用脚本代码访问它.我在下面列出了我的示例.包装功能.这是一个简单的包装程序,我需要返回作为参数传递的字符串.以下是代码.

I am going through the documentation of Qt Scripting and came up to it is totally confusing and full if mis guiding text. Could some please explain in simple English how to wrap a function and access it in script code after wrapping. I have included my example below. Wrapper function. This is a simple wrapper which I need to return the string that is passed as the parameter. following is the code.

#include <QApplication>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptContext>
#include <QDebug>

QScriptValue returnProperty(QScriptContext *context , QScriptEngine *engine)
{
    qDebug() << "running returnValues Function "<< context->argument(0).toString();
    return context->thisObject().property("returnValue");
} 

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QScriptEngine engine;
    //Evaluating a simple expression
    qDebug() << engine.evaluate("1+2").toNumber();

    QScriptValue func = engine.globalObject();
    func.setProperty("foo",engine.newFunction(returnProperty),QScriptValue::PropertyGetter);
    engine.evaluate("var v= foo('name') ; print( 'This prints values from addValues function :',v) ;");
}

输出如下

3
Running returnValues Function  "undefined" 

如果我正确理解了这就是我应该做的,并且如果我调用doc函数中提到的engine.newObject()甚至都没有被调用.

If I am understanding this correctly this is what I should do and if I call engine.newObject() as it is mentioned in the doc function does not even get called.

我不明白的是,我在func.setproperty行中分配的属性是什么,一旦设置了属性foo,该怎么办?如何在函数中设置值.

what I do not get here is that in what is the property I am assigning in func.setproperty line and what can I do with the property foo once I set it. How can I set a value in the function.

如果有人在这里解释我做错了,我将不胜感激.

I appreciate if someone explain what I am doing wrong here.

推荐答案

您已经步入正轨. QScriptEngine :: newFunction()将函数引入引擎.现在,您需要一种从脚本访问此功能的方法.函数"只是全局对象的属性,您可以使用 setProperty()添加新属性.代码

You are already on the right track. QScriptEngine::newFunction() brings the function into the engine. Now, you need a way to access this function from the script. A "function" is just a property of the global object and you can add a new property with setProperty(). The code

QScriptValue globalObject = engine.globalObject();
QScriptValue func = engine.newFunction(returnProperty);
globalObject.setProperty("foo", func);

产生输出

3
running returnValues Function  "name"
This prints values from addValues function : name


仅当您要创建属性(在访问时必须调用函数)时,才需要标记 QScriptValue :: PropertyGetter QScriptValue :: PropertySetter .它类似于 QObject 的属性.考虑以下示例:


The flags QScriptValue::PropertyGetter and QScriptValue::PropertySetter are only needed, when you want to create a property, which has to call a function upon access. It is similar to the properties of QObject. Consider this example:

class MyObject : public QObject
{
   Q_PROPERTY(QString name READ getName WRITE setName)
};
MyObject* obj = new MyObject;

当您执行 obj-> setProperty("name","Sam"); 时,您会在后台调用 MyObject :: setName("Sam") obj-> getProperty("name") MyObject :: getName()的包装.一个小例子:

When you do a obj->setProperty("name", "Sam"); you call MyObject::setName("Sam") in the background and obj->getProperty("name") is a wrapper for MyObject::getName(). A small example:

QScriptValue getName(QScriptContext* ctx, QScriptEngine* eng)
{
     // Return the value of the internal '_name_' property.
     qDebug() << "Getter 'getName' called";
     return ctx->thisObject().property("_name_");
}

QScriptValue setName(QScriptContext* ctx, QScriptEngine* eng)
{
    // Do some processing and store the name in an internal '_name_' property. 
    qDebug() << "Setter 'setName' called";
    ctx->thisObject().setProperty("_name_", 
                                  ctx->argument(0).toString().toUpper());
    return QScriptValue::UndefinedValue;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QScriptEngine engine;
    QScriptValue globalObject = engine.globalObject();

    // Create a new object.
    QScriptValue obj = engine.newObject();
    // Bring the functions into the engine.
    QScriptValue getNameFunc = engine.newFunction(getName);
    QScriptValue setNameFunc = engine.newFunction(setName);
    // Create a 'name' property, which calls the getter and setter from above.
    obj.setProperty("name", getNameFunc, QScriptValue::PropertyGetter);
    obj.setProperty("name", setNameFunc, QScriptValue::PropertySetter);
    // Make the new object known as 'person'.
    globalObject.setProperty("person", obj);

    // Test our construct.
    engine.evaluate("print('Set the name to fitzgerald');");
    engine.evaluate("person.name = 'fitzgerald';");
    engine.evaluate("print('And the name is... ' + person.name)");
}

最后输出:

Set the name to fitzgerald
Setter 'setName' called 
Getter 'getName' called 
And the name is... FITZGERALD

这篇关于Qt脚本功能包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆