1

我试图在将 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;
        }

我可以看到参数“table”正在正确传递,但其余代码不起作用。我想我不能这样做:

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

知道我在做什么错吗?我还应该怎么做才能让它发挥作用?

谢谢,

4

1 回答 1

3

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

有关详细信息,请参阅 Qt 文档:将 C++ 类型的属性暴露给 QML

于 2015-08-20T20:39:36.813 回答