title: Setting text input focus within a QWebView
body: <p>I'm trying to display a Google login dialog in a QWebView, and as I recall, Google likes to set your keyboard focus to the first input field on the page (in this case, the e-mail field).</p>
  
  <p>Unfortunately, the QWebView widget doesn't actually respect this behaviour, and therefore loads the page with keyboard focus on nothing at all:</p>
  
  <p><img src="https://i.stack.imgur.com/M3hD1.jpg" alt="Login dialog with no focus"></p>
  
  <p>So I decided dig about a little, and inserted this code snippet into my class logic:</p>
  
  <pre><code>void GoogleAuthDialog::pageLoaded(bool ok) {
      if (ok) {
          ui-&gt;webView-&gt;setFocus();
          ui-&gt;webView-&gt;page()-&gt;mainFrame()-&gt;setFocus();
          QWebElement el = ui-&gt;webView-&gt;page()-&gt;mainFrame()-&gt;findFirstElement("input:not([type=hidden])");
          if (!el.isNull()) {
              el.setFocus();
              el.evaluateJavaScript("this.focus()");
              el.evaluateJavaScript("this.click()");
          }
      }
  }
  </code></pre>
  
  <p>And the following declaration in my header file:</p>
  
  <pre><code>...
  private slots:
      void pageLoaded(bool);
  </code></pre>
  
  <p>Back in the class code, I connected the appropriate signal from the QWebView to my slot:</p>
  
  <pre><code>connect(ui-&gt;webView, SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded(bool)));
  </code></pre>
  
  <p>Yes, I am throwing every possible thing I can think of at it to redirect keyboard focus to the first input box.</p>
  
  <p>Unfortunately, the code did not seem to work, as while it did focus the right input box, I could not type anything inside of it until I clicked it myself, or pressed Tab:</p>
  
  <p><img src="https://i.stack.imgur.com/ZRrb2.jpg" alt="Focus with no keyboard"></p>
  
  <p>Next I bound the function to my Control key, and proceeded to produce strange results.</p>
  
  <p>If I placed focus into the password field manually, and pressed the Control key, I noticed that I would continue to have keyboard focus in the password field, but have 'visual' focus in the e-mail field:</p>
  
  <p><img src="https://i.stack.imgur.com/temNQ.jpg" alt="Keyboard caret in the wrong field"></p>
  
  <p>Also, when I typed something in this 'state', occasionally a letter might 'leak' into the e-mail field before the visual and keyboard focus would 'reset' to the password field:</p>
  
  <p><img src="https://i.stack.imgur.com/u8YG1.jpg" alt="Character leaking"></p>
  
  <p>Is there a proper way of redirecting keyboard focus to an input field of my choosing?</p>