[PyQt] Type casting

David Boddie dboddie at trolltech.com
Wed Apr 1 13:39:22 BST 2009


On Wed Apr 1 07:40:33 BST 2009, Rendezvous wrote:

> It prints a QWebPage, which doesn't help anyway, since
> calling .currentFrame().url() on it doesn't return the URL of the link that
> was clicked on.

OK, so the signal is actually emitted by the QWebPage rather than from a
QAction. That makes things clearer.

> The code i'm using is basically the PyQt4 version of what 
> the Arora browser uses with a slight modification:
> >From contextMenuEvent:
>
> menu.addAction("Open in New &Tab", self,
> SLOT("openLinkInNewTab()")).setData(QtCore.QVariant(r.linkUrl().toString())
>);
>
> Relative slot:
> @QtCore.pyqtSignature("openLinkInNewTab()")
> def openLinkInNewTab(self):
>         self.emit(SIGNAL("newTab(const QUrl &)"),
> self.sender().data().toString());

Strangely, the sender() is None in my test application, presumably because the
QAction created for the menu no longer exists.

There seem to be two workarounds for this problem:

 1. Use a "short-circuit" connection instead of using SLOT():

    menu.addAction("Open in New &Tab", self.openLinkInNewTab)

 2. Use the return value from menu.exec_() to read the data for the item
    chosen by the user:

    data = menu.exec_().data()

David


More information about the PyQt mailing list