如何将类方法转换为QScriptEngine :: FunctionSignature

时间:2017-09-14 14:50:13

标签: c++ qt function-pointers pointer-to-member qtscript

我想传递这种方法:

QScriptValue ScriptProcessContext::construct(QScriptContext * ctx, QScriptEngine *)
{
    return this->newInstance();
}

QScriptEngine::newFunction。我尝试了以下选项:

  • 与文档and also this example code中一样:

    QScriptValue ctor = engine->newFunction(construct);
    

    错误:

    error C3867: 'ScriptProcessContext::construct': function call missing argument list; use '&ScriptProcessContext::construct' to create a pointer to member
    
  • 强制演员:

    QScriptValue ctor = engine->newFunction((QScriptEngine::FunctionSignature)construct);
    

    错误:

    error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'QScriptEngine::FunctionSignature'
    
  • 将指针强制转换为成员

    QScriptValue ctor = engine->newFunction(
       (QScriptValue(ScriptProcessContext::*)(QScriptContext *, QScriptEngine *))
        &ScriptProcessContext::construct
    );
    

    错误:

     error C2664: 'QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature,int)' : cannot convert parameter 1 from 'QScriptValue (__cdecl ScriptProcessContext::* )(QScriptContext *,QScriptEngine *)' to 'QScriptEngine::FunctionSignature'
    

那么如何正确编写呢?

1 个答案:

答案 0 :(得分:0)

您尝试传递成员函数。那是不支持的。使用自由函数或静态方法。

请注意,在这两种情况下,您都没有<html> <head> <meta charset="utf-8" /> <title>Chart.js demo</title> <script src="./node_modules/chart.js/dist/Chart.min.js"></script> </head> <body> <h1>Chart.js Sample</h1> <div class="chart-container" style="width: 600px; height: 400px"> <canvas id="countries"></canvas> </div> <script> var pieData = { datasets: [{ data: [20, 40, 10, 30], backgroundColor: ["#878BB6", "#4ACAB4", "#FF8153", "#FFEA88"] }] }; // Get the context of the canvas element we want to select var countries = document.getElementById("countries").getContext("2d"); new Chart(countries, { type: 'pie', data: pieData }); </script> </body> </html> 指针。您需要以不需要对象的方式编写函数。