[solved] Does addToJavaScriptWindowObject() support member objects with slots?

  • I'm trying to use QtWebKit to create a JS api. The JavaScript code will call the API like


    I don't have any problems getting api.add("test") to run from JS. It correctly executes the add slot on the api object. If I add a simple QString property to api, I can print the value of it. I cannot seem to get the same behavior for the member objects though.

    It seems the member objects of api do not have their slots connected/exposed to JS ?

    I used Q_PROPERTY() to declare the member objects and also used qRegisterMetaType<T>(T) to register it for using with slots. Am I missing something?

  • I finally solved it. Help for future googlers:

    • you need to use pointers to your objects everywhere, not references (not allowed by QObject, because private copy constructor)
    • register your api as a QVariant : qRegisterMetaType<QVariant>("api");
    • call your api with a helper method:
      QVariant api::test()
      QVariant var(QMetaType::QObjectStar);
      return var;
    • register it with Q_PROPERTY(QVariant test READ test)

    [edit: code highlighted with @ tags / Denis Kormalev]

  • Please mark thread as [solved] and don't forget to highlight your code.

  • A (much) simpler solution turned out to be to use Q_INVOKABLE in the header file + object->setObjectName("name") on the object to make it available under a nice name.

  • Q_INVOKABLE seems to not work. But the Q_PROPERTY sample is ok for me too:

    @QVariant BusinessProxy::lineUp() {
    qDebug() << this->objectName() << ": lineUp called";
    QVariant var(QMetaType::QObjectStar);
    return var;

    with into the constructor:

    @m_lineUp = new DBusLineUp("com.orange.device.BusinessLayerService", "/LineUp", connection, this);

    And into the header:

    @ Q_OBJECT
    Q_PROPERTY(QVariant lineUp READ lineUp)
    QVariant lineUp();
    DBusLineUp *m_lineUp;@

    But I didn't put any 'qRegisterMetaType': I used the 'addToJavaScriptWindowObject' way

Log in to reply