使用QJSEngine将QObject传递给脚本函数?

时间:2015-08-16 22:29:35

标签: qt qml qtscript

我尝试在传递QObject作为参数时调用外部脚本中的函数。

我的QObject定义如下:

#ifndef INSERTVALUES_H
#define INSERTVALUES_H

#include <QObject>

struct insertValueDef
{
  QString name;
  QString xmlCode;
  QString value;
  bool key;
  bool insert;
};
typedef insertValueDef TinsertValueDef;

class insertValues : public QObject
{
    Q_OBJECT
public:
    explicit insertValues(QObject *parent = 0);
    ~insertValues();
    void insertValue(TinsertValueDef value);
    int count();

    void setItemName(int index, QString name);
    void setItemXMLCode(int index, QString xmlCode);
    void setItemValue(int index, QString value);
    void setItemIsKey(int index, bool isKey);
    void setItemToInsert(int index, bool toInsert);

    QString itemName(int index);
    QString itemXMLCode(int index);
    QString itemValue(int index);
    bool itemIsKey(int index);
    bool itemToInsert(int index);

    bool valueIsNumber(int index);
    int getIndexByColumnName(QString name);

private:
    QList<TinsertValueDef> m_insertList;
};

#endif // INSERTVALUES_H

我的JS脚本功能是:

function beforeInsert(table,data)
{
  if (table == "tmpTable")
  {
    var index = data.getIndexByColumnName("tmpfield");
    if (index >= 0)
    {
     data.setItemValue(index,"Carlos Quiros"); 
    }
  }
}

运行脚本的代码如下:

QFile scriptFile(javaScript);
        if (!scriptFile.open(QIODevice::ReadOnly))
        {
            log("Error: Script file defined but cannot be opened");
            return 1;
        }
        JSEngine.evaluate(scriptFile.readAll(), javaScript);
        scriptFile.close();

        insertValues insertObject;

        TinsertValueDef tfield;
        tfield.key = false;
        tfield.name = "tmpfield";
        tfield.xmlCode = "tmpCode";
        tfield.value = "tmpValue";
        tfield.insert = true;
        insertObject.insertValue(tfield);
        QString error;

        beforeInsertFunction = JSEngine.evaluate("beforeInsert",error);

        if (!beforeInsertFunction.isError())
        {
            QJSValue insertListObj = JSEngine.newQObject(&insertObject);
            QJSValue result = beforeInsertFunction.call(QJSValueList() << "tmpTable" << insertListObj);
            if (result.isError())
            {
                log("Error calling BeforInsert JS function.");
                return 1;
            }
            else
            {
                log("JS function seems to be ok");
                for (int pos = 0; pos < insertObject.count(); pos++)
                {
                    log(insertObject.itemName(pos) + "-" + insertObject.itemValue(pos));
                }
                return 1;
            }
        }
        else
        {
            log("Error evaluating BeforInsert JS function. [" + error + "]");
            return 1;
        }

我可以看到参数&#34; table&#34;正常传递,但其余代码不起作用。我想我做不到:

var index = data.getIndexByColumnName("tmpfield");

知道我做错了什么吗?我还应该做些什么来使其发挥作用?

谢谢,

1 个答案:

答案 0 :(得分:3)

要访问属性或调用传递给QObjects(或QML)的QJSEngine方法,您需要使用Q_PROPERTYQ_INVOKABLE宏来声明它们QObject - 派生类声明。

有关详细信息,请参阅Qt文档:Exposing Attributes of C++ Types to QML