Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT Script - assigned struct in script



  • Hello,

    I'm writing program, that have struct with data (only data). This struct has no Q_OBJECT macro and isn't inherit from QObject.
    Something like this:

    struct DATA2{
         QString beta;
         int b;
    };
    
    struct DATA{
         QString alfa;
         int a;
         DATA2 d;
         ......
    };
    

    Now I want use script to process this struct and return QString:

    
        script_engine=new QScriptEngine();
            
        DATA data;
        QScriptValue arg=script_engine->newQObject(&data);
        script_engine->globalObject().setProperty("arg",arg);
    
        QScriptValue val=script_engine->evaluate(script_content);
        if(val.isError()){
            WARNING<<"ERROR: "<<val.property("lineNumber").toInteger()<<"  - "<<val.toString();
        }else{
            INFO<<val.toString();
        }
    
    

    Is there a way to do this? Without write getter function for all records in the structure? Script will only read values.

    Thank you


  • Lifetime Qt Champion

    Hi
    I dont think so.
    It has to be a QObject and use properties.


Log in to reply