[PyQt] problem with tab key to switch focus in Qt-based plugins in QtWebKit

Matthew Scott matt at 11craft.com
Tue Apr 13 01:56:45 BST 2010

I am experiencing strange behavior when embedding Qt-based plugins into web
pages rendered using QtWebKit.

The plugins have child widgets accessible using the tab key, e.g. QLineEdit
widgets.  When there are no tab-accessible elements on the page, it only
takes one keystroke of the tab key to move to the next field.  However, if
you begin adding tab-accessible elements to the page, you have to press the
tab key as many more times as there are those elements in order to switch

For example, the code posted below exhibits this behavior:

- startup:   QLineEdit line1 has keyboard focus
- tab:  does nothing
- tab:  does nothing
- tab:  does nothing
- tab:  moves keyboard focus to QLineEdit line2
- tab:  does nothing
- tab:  does nothing
- tab:  does nothing
- tab:  moves keyboard focus to QLineEdit line3
- tab:  does nothing
- tab:  does nothing
- tab:  does nothing
- tab:  nothing has keyboard focus
- tab:  "somewhere" anchor in HTML has keyboard focus
- tab:  "somewhere else" anchor has keyboard focus
- tab:  "hi" submit button has keyboard focus
- tab:  QLineEdit line1 once again has keyboard focus

If you change the code to add more items in HTML that can get keyboard
focus, you'll find that the length of the series of "tab does nothing"
mentioned above is equal to the number of items in the HTML page that can
get keyboard focus!

Is this an issue others have run into?  I've only created a PyQt version,
and not a C++ version, so I don't know if this is behavior that is due to
PyQt or if it's something deeper within Qt and QtWebKit.

Configurations exhibiting this problem:

- Windows XP SP2
  - Python 2.6.5
  - PyQt 4.7
  - Qt 4.6.1   (also has problem in Qt 4.5 and PyQt 4.6 series)

- Ubuntu 10.4 beta 2
  - Python 2.6.5
  - PyQt 4.7.2
  - Qt 4.6.2

import sys

from PyQt4 import QtCore, QtGui, QtWebKit

HTML = """
      <title>QtWebKit Plug-in Test</title>
      <object type="application/x-qt-plugin" classid="MyCalendarWidget"
name="lineedits" height=300 width=500></object>
      <br />
      <a href="/">Somewhere</a>
      <a href="/foo">Somewhere else</a>
      <input type="submit" value="hello world" />

class MyWebView(QtWebKit.QWebView):

    def __init__(self, parent=None):
        super(MyWebView, self).__init__(parent)
        self._page = MyWebPage(self)

class MyWebPage(QtWebKit.QWebPage):

    def __init__(self, parent=None):
        super(MyWebPage, self).__init__(parent)

    def createPlugin(self, classid, url, paramNames, paramValues):
        return LineEditsWidget(self.view())

class LineEditsWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(LineEditsWidget, self).__init__(parent)
        self._layout = QtGui.QVBoxLayout(self)
        self.line1 = QtGui.QLineEdit(self)
        self.line2 = QtGui.QLineEdit(self)
        self.line3 = QtGui.QLineEdit(self)

    def setLine1(self, s):

    def setLine2(self, s):

    def setLine3(self, s):

app = QtGui.QApplication(sys.argv)

viewer = MyWebView()


Matthew Scott
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100412/2318ea7e/attachment.html>

More information about the PyQt mailing list