QT 脚本QScriptValued对自定义结构体/数组发送与读取的实现

1、定义自定义结构体或者数组

#include 
struct ArrayInt
{
    std::vector m_Array;
};
Q_DECLARE_METATYPE(ArrayInt);

2、制定自定义数组的使用规则及处理逻辑实现,并设置自定义数据

QScriptValue Array(QScriptContext* context, QScriptEngine* engine)
{
    try
    {
        int c_size = context->argumentCount();
        if (c_size != 2)
        {
            QString error = "Array输入参数错误!";
            context->throwError(error);
            return error;
        }
        QVariant q_var1 = context->argument(0).toVariant();
        QString userType0=context->argument(1).toString();
        QStringList qli=q_var1.toStringList();
       QStringList Element= userType0.split(".");
       if(Element.count()!=2)
       {
           QString error = "Array输入操作参数错误!";
           context->throwError(error);
           return error;
       }
       QString userType=Element[0];
        if(userType.toLower()=="std::vector")
        {
            ArrayInt temp_score;
            QVariant t_temp_score;
            for (int n = 0; n < (int)qli.size(); n++)
            {
                temp_score.m_Array.push_back(qli[n].toInt());
            }
        }
    }
    catch (...)
    {
        QString error = "Array输入参数错误!";
        context->throwError(error);
        return error;
    }
}

3、读取自定义结构体/数组里面的数据

         scriptVal = scriptEngine.evaluate(Qtext);//计算公式        
         if(scriptVal.isVariant())
         {
             QVariant q_var =scriptVal.toVariant();             
             if(q_var.userType()== qMetaTypeId())
             {
                 ArrayInt array = q_var.value();
                 int count = array.m_Array.size();
                 for (int id=0;idsetItem(row, 2,item);
                     std::string outstr = std::to_string(array.m_Array[id]);   
                     if(flag) ui->plainTextEdit_log->appendPlainText("输出值:"+string2Qstring(outstr));
                 }
             }
        }

你可能感兴趣的:(QT,qt,开发语言,c++,算法,计算机视觉,opencv)