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. [SOLVED] Question about addToJavaScriptWindowObject
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Question about addToJavaScriptWindowObject

Scheduled Pinned Locked Moved Qt WebKit
3 Posts 2 Posters 1.2k 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
    dolevo
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      You must declare mpSetValue as a slot.

      157

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

        Excellent!!! It works now.

        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