[PyQt] Monkeypatching QWebPage.userAgentForUrl
David Boddie
david at boddie.org.uk
Fri May 13 16:34:36 BST 2011
On Fri May 13 02:23:30 BST 2011, Kay Hayen wrote:
> >> thanks in advance for any explanations or suggestions how to change the
> >> user agent 'globally'
> >
> > Why do you try to monkeypatch it? Subclassing QWebView and implementing
> > just the overridden method is much cleaner and doesn't look like a hack.
>
> Also if you my message about the SIP issue, it appears that the PyQt
> core will not consider any overload it doesn't detect as being such at
> class creation time.
I think this QWebPage issue is just a misunderstanding about what kind of
objects are being passed around. The objects people create from within
Python are subclasses of Qt widgets with extra hooks to enable method
reimplementation from within Python, and this includes support for
monkey-patching of both classes and instances.
> I almost clearly doubt, that attribute assignment will modify the class
> later on.
Here's a working example:
from PyQt4.QtGui import *
app = QApplication([])
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.fillRect(event.rect(), QBrush(QColor(255,0,0)))
painter.end()
QWidget.paintEvent = paintEvent
w = QWidget()
w.show()
app.exec_()
> In general, it appears, that to overload you must use a class and for me
> unfortunately, it must be a pure Python function or method object at
> that point.
I haven't looked at this in any detail, but you may be correct in saying that
the hooks that allow method overriding are specifically looking for certain
kinds of objects; in this case, Python callables.
> Enhancing sip to be able to track at run time the changes to the class
> appears possible. For consistency with Python it should be done, but the
> benefit, other than consistency with every other Python class, I admit,
> is dubious.
I haven't looked at this, either. I think sip already does part of what you
want. I'll re-read your earlier message and try to understand what you were
looking at.
David
More information about the PyQt
mailing list