[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
fields.

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 = """
<html>
   <head>
      <title>QtWebKit Plug-in Test</title>
   </head>
   <body>
      <object type="application/x-qt-plugin" classid="MyCalendarWidget"
name="lineedits" height=300 width=500></object>
      <script>
         lineedits.setLine1('line1');
         lineedits.setLine2('line2');
         lineedits.setLine3('line3');
      </script>
      <br />
      <a href="/">Somewhere</a>
      <a href="/foo">Somewhere else</a>
      <input type="submit" value="hello world" />
   </body>
</html>
"""


class MyWebView(QtWebKit.QWebView):

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


class MyWebPage(QtWebKit.QWebPage):

    def __init__(self, parent=None):
        super(MyWebPage, self).__init__(parent)
        self.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,
True)

    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.setLayout(self._layout)
        self.line1 = QtGui.QLineEdit(self)
        self.line2 = QtGui.QLineEdit(self)
        self.line3 = QtGui.QLineEdit(self)
        self._layout.addWidget(self.line1)
        self._layout.addWidget(self.line2)
        self._layout.addWidget(self.line3)

    @QtCore.pyqtSlot(str)
    def setLine1(self, s):
        self.line1.setText(s)

    @QtCore.pyqtSlot(str)
    def setLine2(self, s):
        self.line2.setText(s)

    @QtCore.pyqtSlot(str)
    def setLine3(self, s):
        self.line3.setText(s)


app = QtGui.QApplication(sys.argv)

viewer = MyWebView()
viewer.setHtml(HTML)
viewer.show()

app.exec_()



-- 
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