[PyKDE] Event chaining and passing parameters

Timothy Grant timothy.grant at gmail.com
Sat Sep 18 22:45:08 BST 2004


On Fri, 17 Sep 2004 03:40:12 -0700, David Boddie <david at boddie.org.uk> wrote:
> On Thu, 16 Sep 2004 12:57:55, Jim Bublitz wrote:
> 
> >On Thursday 16 September 2004 12:22, Timothy Grant wrote:
> 
> >> There are two comments in the code showing what I'd like to accomplish
>  
> [...]
> 
> >>     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.
>  
> You can also store the item in the popup menu and retrieve it later:
> 
> 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 
>         self.item = item
>         self.exec_loop(pos)
> 
> [In the main window class]
> 
>     def edit(self):
>         print "editing" 
>         # The sender is the action. Its parent is the popup menu.
>         # We recorded the list item in the popup menu.
>         print self.sender().parent().item
> 
> This is a little indirect, so it might be better to use the popup menu's
> activated signal rather than the signal from the action:
> 
> from qt import *
> 
> 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 
>         self.item = item
>         self.exec_loop(pos)
> 
> class DetailList(QListView):
>     def __init__(self, parent):
>         QListView.__init__(self, parent)
>         self.addColumn("Items")
>         for i in range(0, 10):
>             item = QListViewItem(self)
>             item.setText(0, "Item %i" % i)
> 
> 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)
>  
>         # Use a signal from the popup menu rather than from the
>         # action itself.
>         self.connect(self.context_menu, SIGNAL('activated(int)'), self.edit)
>         self.setCentralWidget(self.list)
> 
>     def edit(self):
>         print "editing" 
>         # The sender is now the popup menu. We recorded the item inside the
>         # menu.
>         print self.sender().item, self.sender().item.text(0)
> 
> Hope this helps,
> 

This was absolutely fantastically helpful. Thank you so much for
showing me how it works.

Greatly appreciated.



-- 
Stand Fast,
    tjg.




More information about the PyQt mailing list