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,...]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值