Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtScript: getting variable name which calls Qt/C++ slot out of QtScript
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JSchmidt
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JSchmidt
      wrote on last edited by
      #2

      Has anyone a suggestion?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved