Please nominate your Qt Champions for 2021!

C++ and QtScript, overload a function

  • Going to start this out with saying, I am not sure if this can be done or not, so I'm asking here. Using QT 4.6.3 and the qtscriptbindings from the labs.

    I create a C++ class (TestClass) with a function doSomething()

    I pass the C++ into the QscriptEngine as "myObject" like so
    @var test = new TestClass();
    QScriptValue objectValue = engine->newQObject(test);
    engine->globalObject().setProperty("myObject", objectValue);@

    Now what I want to do is in the QtScript overload that function doSomething(). Is this possible?

    @myObject.prototype.doSomething = function(){
    print("yeah it works");

    Something along those lines. That way if a signal was connected in the C++ to the C++ slot, I could either override it or add to it.

  • I'm not sure, but I think, it don't work, because IMHO when you create global object you get follow hierarchal structure:

    myObject = function() {
    this.doSomething = function() {
    // Some commands from C++

    myObject.prototype.doSomething = function(){
    print("yeah it works");

    i.e. direct object method overlap prototype method.
    But I recommend you test it.

  • Yeah I tried it the way I had posted and I get the whole.
    myObject.prototype' [undefined] is not an object

    If I try it with the
    @myObject.proto.doSomething = function(){
    print("yeah it works");

    Then I don't get the same error, script takes it, but I'm still not 100% clear on the usage of proto and it doesn't work for what I am trying to do.

Log in to reply