[SOLVED] Question about addToJavaScriptWindowObject



  • Hi all,

    I have created a class whose function is going to be called from inside of a javascript. Here is my class:

    @class web1: public QObject
    {
    public:
    web1() {};

    void mpSetValue(int value) { qDebug() << value;}
    

    };@

    I also created a slot which is going to be fired when mainFrame emits javaScriptWindowObjectCleared like below:
    @ connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
    this, SLOT(populateJavaScriptWindowObject()));@

    and here is my slot:

    @void engine2dehands::populateJavaScriptWindowObject(void)
    {
    qDebug() << "---==>> populateJavaScriptWindowObject";
    webView->page()->mainFrame()->addToJavaScriptWindowObject("webb", wb);
    }@

    Here is the code where I run my [removed]

    @ QWebElement aAfdeling = aElement.findFirst("select[id='dropdownlist']");

    QString jsCode = QString("var exists = false;"
                     "for(var i = 0, opts = document.getElementById('dropdownlist').options; i < opts.length; ++i)"
                     "   if( opts[i].value === '%1' )"
                     "   {"
                     "        alert&#40;\"kids found in dropdown menu\"&#41;;"
                     "    webb.mpSetValue(i&#41;;"
                     "    break;"
                     " }").arg("kids");
    
    qDebug() << jsCode;
    aElement.evaluateJavaScript(jsCode);@
    

    There is a dropdown menu in the target webpage and above function finds the order of the item in this dropdown list. The javascript function work perfect since I get "kids found in dropdown menu" message. right after that message, I should get a debug message with the order, but I get nothing. populateJavaScriptWindowObject slot is also called and I see the debug log in there as well. Could anyone tell me how I can get the value out? Where am I doing wrong?


  • Moderators

    Hi,

    You must declare mpSetValue as a slot.



  • Excellent!!! It works now.


Log in to reply
 

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