[PyQt] Type casting

Rendezvous junk_and_mail at yahoo.com
Wed Apr 1 07:40:33 BST 2009

--- On Tue, 3/31/09, David Boddie <dboddie at trolltech.com> wrote:

> From: David Boddie <dboddie at trolltech.com>
> Subject: Re: [PyQt] Type casting
> To: pyqt at riverbankcomputing.com
> Date: Tuesday, March 31, 2009, 2:55 PM
> -----Inline Attachment Follows-----
> On Tue Mar 31 19:39:56 BST 2009,
> Rendezvous wrote:
> > I am using Qt4.5 and PyQt4 and i have subclassed a
> QWebView with a custom
> > contextMenuEvent, and one of the actions of the menu
> contains the URL of
> > the link that the action was called for. Without
> having to perhaps subclass
> > QAction, i use QObject.sender() in attempt to retrieve
> the QAction that was
> > clicked on. However, i get an AttributeError, that
> there is no attribute
> > called data, where there would have been had it been a
> QAction. Unless
> > there's another way of achieving what i want without
> perhaps subclassing
> > QAction (i.e. if subclassing would indeed help), then
> knowing how to type
> > cast a QObject to a QAction would be very helpful for
> my purpose.
> Can you print to the console what QObject.sender() returns?
> Would it also be
> possible for you to show some code so that we can see
> exactly what you are
> doing?
> David

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. 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:
def openLinkInNewTab(self):
        self.emit(SIGNAL("newTab(const QUrl &)"), self.sender().data().toString());


More information about the PyQt mailing list