Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Obtain return value of a JS function, QtWebkit bridge
Forum Updated to NodeBB v4.3 + New Features

Obtain return value of a JS function, QtWebkit bridge

Scheduled Pinned Locked Moved Qt WebKit
4 Posts 2 Posters 3.1k 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.
  • D Offline
    D Offline
    Deschain
    wrote on last edited by
    #1

    I'm (trying) developing a wrap for a JS framework and my problem is to access the values returned by JS functions in the original framework.

    The idea would be to connect the C++ method with the JS function but in order to do so the method has to be a signal and signals can not handle return values. The proper way to do so is to use the macro Q_INVOKABLE but in that case I can not connect it to the JS function.

    The JS code would look like something like this
    @
    //Somewhere in the JS framework
    JSFramework.myclass.prototype = {
    equals : function(obj) {
    if(!obj) { return false;}
    return true
    },
    //more methods...
    }
    @

    And my C++ code would look like this
    @
    class myclass : public QObject {
    Q_OBJECT

    public:

    myclass();
    ~myclass();

    //Invocable method can handle return values according to documentation.
    //But they can not be connected to js functions
    Q_INVOKABLE bool invokable_equals(myclass* obj);

    signals:

    //Signals can be connected to js method but can not handle return values
    bool signal_equals(myclass* obj);
    }
    @

    So what I am trying to do is to call the JS function equals when the the C++ method is called. Signals can be connected to JS functions but can not handle return values. Invokable methods can not be connected to JS functions but can handle return values.
    @
    //In the js
    myobject_name_on_js.signal_equals.connect(equals) //Ok
    myobject_name_on_js.invokable_equals.connect(equals) //Error, invokable_equals() is not a signal
    @
    ¿Any ideas about this issue?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Pt_develop
      wrote on last edited by
      #2

      I don't quite understand your question as I'm just learning the bridge technology myself and have not attempted to create signals or slots connected to js but I have been able to get return values using Q_INVOKABLE functions in js for primitives such as strings. The method I used is as follows:

      Create a Object such as
      @class myClass : public QObject
      {
      Q_OBJECT
      public:
      explicit myClass(QObject *parent=0);
      Q_INVOKABLE QString myMethod(QString text);
      };@

      Connect the QWebFrame signal javaScriptWindowObjectCleared() to a slot that includes code like

      @myClass *myOBJ = new myClass(this);
      page()->mainFrame()->addToJavaScriptWindowObject("myOBJ",myOBJ);@

      In your js myOBJ can be treated as any other js object. For example:
      @var myString = myOBJ.myMethod(text)@

      will return a string that is a function of how the string "text" is manipulated by myMethod. This works for boolean and integer primitives as well.

      According to the "documentation":http://harmattan-dev.nokia.com/docs/library/html/qt4/qtwebkit-bridge.html this method should be valid for other data types and even containers (e.g., QVariantMap) also but I haven't been successful in using it to obtain things like lists (especially node lists) yet but am working on it.

      Hope the above helps and look forward to you posting your solution.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Deschain
        wrote on last edited by
        #3

        My problem is that I need to recover the value returned by a js fuction from a C++ method and connect that method to a js function.

        bq. In your js myOBJ can be treated as any other js object. For example:
        @var myString = myOBJ.myMethod(text)@

        In the previous example, your js variable will store the value returned by a C++ method and my problem is just the other way around.

        I need to wrap a js "class" which has its own methods. It's easy to do so with events (calling a C++ method from JS function) and methods that do not return a value (calling a JS function from a C++ method). The problem is, on the second case, if the JS function returns a value and I need that value on the C++ side I can not access that value because it's done by connection signals to js functions and signals can not handle return values.

        In sumary

        • I need to connect a C++ method with a JS function
        • I need the value returned by the JS function to by returned by my C++ method
        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pt_develop
          wrote on last edited by
          #4

          If I understand correctly I think what can help you is the evaluateJavaScript method which returns the js function return value as a QVariant.

          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