Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将回调作为参数发送到QJSValue::callAsConstructor()_Javascript_C++_Qt_Qml_Qt5 - Fatal编程技术网

Javascript 将回调作为参数发送到QJSValue::callAsConstructor()

Javascript 将回调作为参数发送到QJSValue::callAsConstructor(),javascript,c++,qt,qml,qt5,Javascript,C++,Qt,Qml,Qt5,当尝试使用QJSEngine从C++调用JavaScript函数时,其值存储函数的所有参数属性都会神秘地丢失。为什么会这样?有没有办法解决这个问题?如果您试图传递一个没有对象的函数,那么未定义的值也会被传输 qjsengine bug QT += core qml quick quickcontrols2 TARGET = qjsengine-bug TEMPLATE = app DEFINES += QT_DEPRECATED_WARNINGS CONFIG += c++11 console

当尝试使用QJSEngine从C++调用JavaScript函数时,其值存储函数的所有参数属性都会神秘地丢失。为什么会这样?有没有办法解决这个问题?如果您试图传递一个没有对象的函数,那么未定义的值也会被传输

qjsengine bug

QT += core qml quick quickcontrols2
TARGET = qjsengine-bug
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11 console
SOURCES += main.cpp
main.cpp

#include <QCoreApplication>
#include <QJSValueIterator>
#include <QJSEngine>
#include <QJSValue>
#include <QtGlobal>

#include <iostream>

void myMessageOutput(
    QtMsgType t,
    const QMessageLogContext &c,
    const QString &msg
) {
    Q_UNUSED(t);
    Q_UNUSED(c);
    std::cout << msg.toStdString() << "\n";
}

int main(int argc, char *argv[]) {
    qInstallMessageHandler(myMessageOutput);
    QCoreApplication app(argc, argv);

    QJSEngine engine;
    engine.installExtensions(QJSEngine::ConsoleExtension);
    QJSValue constructor = engine.evaluate(
        "(function Component(props){console.log(JSON.stringify(props))})"
    );

    QJSValue callBack = engine.evaluate("(function(text){console.log(text)})");
    callBack.call({"There is no error. Valid JavaScript code..."});

    callBack.call({"Let's create an object, add a couple of props to it"});

    QJSValue object = engine.newObject();
    object.setProperty("First", 1);
    object.setProperty("Second", callBack);
    object.setProperty("Third", "#2");

    QJSValueIterator iter(object);
    while (iter.hasNext()) {
        iter.next();
        callBack.call({
            QString("name: %1, value: %2")
                .arg(iter.name())
                .arg(iter.value().toString())
        });
    }

    callBack.call({"Correct. Three fields"});
    callBack.call({"Let's try to pass an object to the constructor parameters"});

    constructor.callAsConstructor({object});

    callBack.call({"Where did the second property go?"});

    return app.exec();
}
#包括
#包括
#包括
#包括
#包括
#包括
无效myMessageOutput(
QtMsgType t,
常量QMessageLogContext&c,
常量QString和msg
) {
Q_(t);
Q_(c);
std::cout根据定义,没有任何东西来表示函数,因此
JSON.stringify
函数作为属性将导致“缺少”属性(或数组中的函数或函数)