QtScript: getting variable name which calls Qt/C++ slot out of QtScript



  • I have a QScriptEngine and some C++ Objects which I want (and can) create out of QtScript:

    @
    //Some C++ Object
    class SomeObject() : public QObject, public QScriptable //QScriptable only needed for section ('How far I reached')
    {
    Q_Object
    private:
    ...
    ...
    public:
    SomeObject() { //ctor
    qDebug() << "A new SomeObject has been born!";
    }
    public slots:
    void printMyScriptVarName() {
    qDebug() << "yaaiy!";
    }
    ...
    signals:
    ...
    ...
    }

    Q_DECLARE_METATYPE(SomeObject*)
    Q_SCRIPT_DECLARE_QMETAOBJECT(SomeObject, QObject*)
    @
    @
    // Setup of QScriptEngine
    class MyScript
    {
    private:
    QScriptEngine m_engine;

    public:
    MyScript() {
    QScriptValue scriptSomeObject = this->m_engine.scriptValueFromQMetaObject<SomeObject>();
    this->m_engine.globalObject().setProperty("SomeObject", scriptSomeObject);
    }

    public slots:
    void evaluateCode(QString & code);
    ...
    }
    @

    With this setup I can execute the following QtScript Code:
    @
    //QtScript Code Example 1
    var a = new SomeObject();
    a.printMyScriptVarName();
    @

    and get the output as suspected:

    @
    A new SomeObject has been born!
    yaaiy!
    @

    What I want to achieve:
    Now, with the slot 'printMyScriptVarName()' I want to achieve, that I get the 'QtScript variable name' of that SomeObject object, I called from - which would be 'a' in the case of the 'QtScript code example 1' above, because of the line @var a = new SomeObject();@

    How far I reached:
    I can get all variable names in my Script, which hold a reference to my SomeObject object with the following C++ code:
    @
    void SomeObject::printMyScriptVarName()
    {
    /* iterator will iterate over every element, which is in globalObject().. so are may variables I define at runtime */
    QScriptValueIterator it( this->engine()->globalObject() );

    while (it.hasNext())
    {
    it.next();

    //examine only elements which are set to be enumeratable (hides some internal values)
    if (it.flags() & QScriptValue::SkipInEnumeration)  
      continue;
    
    // try to cast the current script element to a SomeObject pointer
    A * pointer = qobject_cast<SomeObject*>( it.value().toQObject() );
    
    // if casting has been successfull and is pointing at me...
    if (pointer && pointer == this)
      qDebug() << "var" << it.name() << "has a reference to me";
    

    }
    }
    @

    using the code base, I can evalutate QtScript code like this:
    @
    //QtScript code example 2
    var a = new SomeObject();
    var b = new SomeObject();
    var c = a;
    var d = b;

    b.printMyScriptVarName();
    @

    and will get the output:
    @
    var 'b' has a reference to me
    var 'd' has a reference to me
    @

    Problem with my Solution:
    I wanted to get the actual caller variable 'b' because variable 'b' calls my slot at line 7. And not all possibile callers (references).

    Has anyone an idea?



  • Has anyone a suggestion?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.