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. [Merged] Problems with QScript
Forum Updated to NodeBB v4.3 + New Features

[Merged] Problems with QScript

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.8k 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.
  • I Offline
    I Offline
    inopportuno
    wrote on last edited by
    #1

    Hi guys,
    It's all the day that I'm trying to make this code working. It should be the same code presented in the QScript "help page":http://doc.trolltech.com/4.6/scripting.html#prototype-based-programming-with-the-qtscript-c-api but unfortunately it doesn't work at all!

    @class Person
    {
    public:
    QString nm;

    Person()
    {

    }

    Person(QString& name)
    :nm(name)
    {

    }
    };

    Q_DECLARE_METATYPE(Person)
    Q_DECLARE_METATYPE(Person*)

    QScriptValue Person_ctor(QScriptContext* c,QScriptEngine* e)
    {
    QString x = c->argument(0).toString();
    return e->toScriptValue(Person(x));
    }

    QScriptValue Person_prototype_toString(QScriptContext* c,QScriptEngine* e)
    {
    Person* per = qscriptvalue_cast<Person*>(c->thisObject());
    qDebug(qPrintable(per->nm));
    return e->undefinedValue();
    }

    ....
    QScriptValue per_ctr = eng->newFunction(Person_ctor);
    per_ctr.property("prototype").setProperty("toString",eng->newFunction(Person_prototype_toString));
    per_ctr.property("prototype").setProperty("myPrint",eng->newFunction(Person_prototype_toString));
    eng->globalObject().setProperty("Person",per_ctr);
    ...@

    If I try to evaluate the following code in JavaScript

    @var p = new Person("Guido");
    p.toString();
    p.myPrint();@

    I should obtain:

    @Guido
    Guido@

    instead what I really obtain is a white string from the toString function (probabily is calling the Object.toString function) and a "Interpreter Error: line 2: TypeError: Result of expression 'p.myPrint' [undefined] is not a function." error message from myPrint. I suppose that I didn't connect correctly the two functions to the Person prototype even if I tried to follow litteraly the documentation pages...PLEASE Could someone explains me what is my fault?!? Thanks!

    1 Reply Last reply
    0
    • I Offline
      I Offline
      inopportuno
      wrote on last edited by
      #2

      If I try to check the presence of the property myPrint from the C++ side with

      @per_ctr.property("prototype").property("myPrint").call();@

      the function is called (even if it clearly crashes because it's not called on a valid object). But this means that at least in the c++ side of the application the myPrint function exists in the Person's prototype. I cannot say the same thing in the JavaScript side when i get an Interpreter Error: line 2: TypeError: Result of expression 'v.myPrint' [undefined] is not a function.

      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