Help! Cannot invoke methods of QQmlPropertyMap object added to Webkit.



  • Please looks at the following codes:

    mainwindow.h:
    @
    class ObjectA : public QObject
    {
    Q_OBJECT
    public:
    ObjectA(QObject *parent = 0) : QObject(parent)
    {
    QVariant vName("ObjectAAAAAA");
    setProperty("name", vName);
    }

    Q_INVOKABLE void Call()
    {
        qDebug("ObjectA::Call() be invoked");
    }
    

    };

    class ObjectB : public QQmlPropertyMap
    {
    Q_OBJECT
    public:
    ObjectB(QObject *parent = 0) : QQmlPropertyMap(parent)
    {
    QVariant vName("ObjectBBBBBBB");
    insert("name", vName);
    }

    Q_INVOKABLE void Call()
    {
        qDebug("ObjectB::Call() be invoked");
    }
    

    };

    class MainWindow : public QGraphicsView
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public slots:
    void AddJavascriptWindowObject();

    private:
    QGraphicsScene *m_pScene;
    QGraphicsWebView m_view;
    QWebFrame *m_mainframe;

    ObjectA m_objA;
    ObjectB m_objB;
    

    };
    @

    =====================
    mainwindow.cpp:

    @
    MainWindow::MainWindow(QWidget *parent)
    : QGraphicsView(NULL, parent)
    {
    QWebSettings *settings = QWebSettings::globalSettings();
    settings->setAttribute(QWebSettings::JavascriptEnabled,true);

    m_pScene = new QGraphicsScene(this);
    m_pScene->addItem( &m_view );
    this->setScene( m_pScene );
    this->resize(640,480);
    m_mainframe = m_view.page()->mainFrame();
    
    connect(this->m_mainframe, SIGNAL(javaScriptWindowObjectCleared()),
            this, SLOT(AddJavascriptWindowObject()));
    
    m_view.setUrl( QUrl(QStringLiteral("qrc:///Test.html")) );
    

    }

    void MainWindow::AddJavascriptWindowObject()
    {
    this->m_mainframe->addToJavaScriptWindowObject("objectA", &m_objA);
    this->m_mainframe->addToJavaScriptWindowObject("objectB", &m_objB);
    }
    @

    ============
    Test.html:

    [removed]

    function CallObjectA(){
    alert(window.objectA.name);
    window.objectA.Call();
    }

    function CallObjectB(){
    alert(window.objectB.name);
    window.objectB.Call();
    }

    [removed]

    <input type="button" value="Call Object A" >
    <input type="button" value="Call Object B" >

    ============
    My question:

    The Call() method of object B cannot be invoked! But the same method of object A is work.
    And, it so strange that the custom property 'name' of object B can be visited !!

    The different of class A and B is just the parent class, A inherited from QObject directly, but B inherited from QQmlPropertyMap(which inherited from QObject).

    Why do this sample? Because my project plan to support Mac/Windows(using QtWebkit) and Andriod/iOS(using QML), I wanna to expose the same C++ object(including properties, methods) to Javascript and QML.

    Thanks a lot.

    [edit: Added missing coding tags @ SGaist]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.