QCAD C++ & JavaScript 混合编程

JavaScript 调用 C++ 函数

QCAD源码工程模块 src\\scripting\\ecmaapi 重新封装了底层模块 coreentitygui等的函数接口,供应用层(界面)ECMAJavaScript调用。

示例:

  1. 在源码src\\core\\RDocument中添加三个public函数及实现。
void addCustomTables(const QString& group, REntity::Id id);
QSet<REntity::Id> getCustomTableValues(const QString& group);
QSet<QString> getCustomTableKeys();
  1. 相应的在源码src\\scripting\\ecmaapi\\generated\\REcmaDocument中添加对应的三个函数及实现。

.h

static  QScriptValue addCustomTables(QScriptContext* context, QScriptEngine* engine);
static  QScriptValue getCustomTableKeys(QScriptContext* context, QScriptEngine* engine);
static  QScriptValue getCustomTableValues(QScriptContext* context, QScriptEngine* engine);

.cpp

  • 注册:
REcmaHelper::registerFunction(&engine, proto, addCustomTables, "addCustomTables");
REcmaHelper::registerFunction(&engine, proto, getCustomTableKeys, "getCustomTableKeys");
REcmaHelper::registerFunction(&engine, proto, getCustomTableValues,"getCustomTableValues");
  • 转换调用:
QScriptValue REcmaDocument::getCustomTableKeys(QScriptContext* context, QScriptEngine* engine)     
{
	//REcmaHelper::functionStart("REcmaDocument::getCustomTableKeys", context, engine);
	//qDebug() << "ECMAScript WRAPPER: REcmaDocument::getCustomTableKeys";
	//QCoreApplication::processEvents();

	QScriptValue result = engine->undefinedValue();
	
	// public function: can be called from ECMA wrapper of ECMA shell:
	RDocument* self = getSelf("getCustomTableKeys", context);
  
	//Q_ASSERT(self!=NULL);
	if (self==NULL) {
		return REcmaHelper::throwError("self is NULL", context);
	}
    if( context->argumentCount() ==0){
    // prepare arguments:
    
    // end of arguments

    // call C++ function:
    // return type 'QSet < QString >'
    QSet <QString> cppResult =self->getCustomTableKeys();
        // return type: QSet < QString >
        // QSet (convert to QVariantList):
     result = REcmaHelper::setToScriptValue(engine, cppResult);

                
    } else
	{
	   return REcmaHelper::throwError("Wrong number/types of arguments for RDocument.getCustomTableKeys().",context);
	}
     //REcmaHelper::functionEnd("REcmaDocument::getCustomTableKeys", context, engine);
     return result;
}
QScriptValue REcmaDocument::getCustomTableValues(QScriptContext* context, QScriptEngine* engine)    
{
	//REcmaHelper::functionStart("REcmaDocument::getCustomTableValues", context, engine);
	//qDebug() << "ECMAScript WRAPPER: REcmaDocument::getCustomTableValues";
	//QCoreApplication::processEvents();

	QScriptValue result = engine->undefinedValue();
	
	// public function: can be called from ECMA wrapper of ECMA shell:
	RDocument* self = getSelf("getCustomTableValues", context);
  
	//Q_ASSERT(self!=NULL);
	if (self==NULL) {
		return REcmaHelper::throwError("self is NULL", context);
	}
    if( context->argumentCount() ==1 && (context->argument(0).isString())){
    // prepare arguments:
  // argument isStandardType
	QString a0 =(QString)context->argument(0).toString();
                
    // end of arguments

    // call C++ function:
    // return type 'QSet<REntity::Id>'
    QSet<REntity::Id> cppResult =self->getCustomTableValues(a0);
        // return type: QSet < REntity::Id >
        // QSet (convert to QVariantList):
    result = REcmaHelper::setToScriptValue(engine, cppResult);

                
    } else
	{
	   return REcmaHelper::throwError("Wrong number/types of arguments for RDocument.getCustomTableValues().",context);
	}
     //REcmaHelper::functionEnd("REcmaDocument::getCustomTableValues", context, engine);
     return result;
}
QScriptValue REcmaDocument::addCustomTables(QScriptContext* context, QScriptEngine* engine)    
{
	//REcmaHelper::functionStart("REcmaDocument::addCustomTables", context, engine);
	//qDebug() << "ECMAScript WRAPPER: REcmaDocument::addCustomTables";
	//QCoreApplication::processEvents();

	QScriptValue result = engine->undefinedValue();
	
	// public function: can be called from ECMA wrapper of ECMA shell:
	RDocument* self = getSelf("addCustomTables", context);
  
	//Q_ASSERT(self!=NULL);
	if (self==NULL) {
		return REcmaHelper::throwError("self is NULL", context);
	}
    if( context->argumentCount() ==2 && (context->argument(0).isString())
		&& (context->argument(1).isNumber())){
    // prepare arguments:
    // argument isStandardType
	QString a0 =(QString)context->argument(0).toString();
    REntity::Id a1 =(REntity::Id)(int)context->argument( 1 ).toNumber();           
    // end of arguments

    // call C++ function:
    // void
    self->addCustomTables(a0,a1);
    }else
	{
	   return REcmaHelper::throwError("Wrong number/types of arguments for RDocument.addCustomTables().",context);
	}
	return result;
}
  1. ECMAJavaScript调用
var doc=EAction.getDocument();
doc.addCustomTables("扩展",1000);
var ids=doc.getCustomTableValues("扩展");
// ids=[1000]
var keys=doc.getCustomTableKeys();
// keys=[Name,Sex,...]

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片