2012-09-07 89 views
1

我正在閱讀Qt腳本編寫的文檔,並提出它是完全混淆和充分,如果錯誤指導文本。有些人可以用簡單的英語解釋如何包裝一個函數並在包裝後用腳本代碼訪問它。下面列出了我的例子。 包裝功能。這是一個簡單的包裝,我需要返回作爲參數傳遞的字符串。以下是代碼。Qt腳本函數包裝

#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" 

如果我正確地理解這個,這是我應該做的,如果我叫engine.newObject(),因爲它在文檔的功能被提及呢甚至沒有被召喚。

我在這裏沒有得到的是,我在func.setproperty行中分配的屬性是什麼,以及我在設置屬性foo後該怎麼做。我如何在函數中設置一個值。

我很感謝,如果有人解釋我在做什麼錯在這裏。

回答

7

您已經走上正軌。 QScriptEngine::newFunction()將該功能引入引擎。現在,您需要一種從腳本訪問此功能的方法。 「函數」只是全局對象的屬性,您可以使用setProperty()添加新屬性。該代碼

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::PropertyGetterQScriptValue::PropertySetter只需要,當你想創建一個屬性,它具有在訪問時調用的函數。它類似於QObject的屬性。考慮下面這個例子:

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

當你調用後臺MyObject::setName("Sam")obj->getProperty("name")一個obj->setProperty("name", "Sam");MyObject::getName()的包裝。一個小例子:

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 
+0

這是一個偉大的description..Thank你... – Tharanga