[PyKDE] Event chaining and passing parameters

Jim Bublitz jbublitz at nwinternet.com
Thu Sep 16 20:57:55 BST 2004


On Thursday 16 September 2004 12:22, Timothy Grant wrote:
> On Thu, 16 Sep 2004 05:18:25 -0700, David Boddie <david at boddie.org.uk> 
wrote:
> > On Wed, 15 Sep 2004 13:54:04, Timothy Grant wrote:
> > > I have QListView that connects to a context menu (QPopupMenu) and
> > > passes the item(s) to my showPopup method.
> > >
> > > The QActions that populate the QpopupMenu then call their own
> > > processing methods.
> >
> > You might need to post some code that shows what you're doing.
> >
> > > My problem is that I don't know how to pass the QListViewItem(s) from
> > > my showPopup method further down the call chain so that say the
> > > editMethod knows which items need to be edited.
> > >
> > > I guess consisely put it is "How do I pass state data along the Event
> > > call chain?"
> >
> > With signals and slots you can find out which object emitted the signal
> > by calling self.sender() in the slot that receives it. You might be able
> > to record this somewhere visible to your editMethod, although you don't
> > say which object would have this method.
> >
> > If you post some fairly concise example code, we can probably work
> > something out.
> >
> > David
>
> OK, here is some *seriously* cut down code that probably doesn't even
> work at the moment but which I hope illustrates the problem.
>
> There are two comments in the code showing what I'd like to accomplish

> class ContextPopupMenu(QPopupMenu):
>     def __init__(self, *args):
>         apply(QPopupMenu.__init__, (self,) + args)

>         self.editAction = QAction(self)
>         self.editAction.setText('Edit')
>         self.editAction.setMenuText('&Edit')
>         self.editAction.setStatusTip('Edit Stuff')
>         self.editAction.addTo(self)

>     def showMenu(self, item, pos):
>         #item is very useful here

           item.listView ().setSelected (item, True)

>         self.exec_loop(pos)

> class VagabondWindow(QMainWindow):
>     def __init__(self, library, *args):
>         apply(QMainWindow.__init__, (self,) + args)
>
>         self.context_menu = ContextPopupMenu()
>
>         self.list = DetailList(self)
>
>         self.connect(self.list,
>                      SIGNAL('contextMenuRequested(QListViewItem *,
> const QPoint &, int)'),
>                      self.context_menu.showMenu)
>
>         self.connect(self.context_menu.editAction,
>                      SIGNAL('activated()'),
>                      self.edit)
>
>     def edit(self):
>         print "editing"
>         #how do I get item down here?

           item = self.list.selectedItem ()

setCurrentItem/currentItem would also work. If you don't want the selected or 
current items to change, you'd need to create your own variable (probably in 
DetailList?) and then make ContextPopupMenu aware of the the DetailList 
instance it corresponds to (via its __init__ method) so it can set the value 
of the variable - basically the same thing.

Jim




More information about the PyQt mailing list