tags:

views:

380

answers:

2
+1  Q: 

PyQt context menu

Hi guys,

I'm adding a contextmenu to a QTableWidget dynamically:

playlistContenxt = QAction("Add to %s" % (currentItem.text()), self.musicTable)
playlistContenxt.setData(currentData)
self.connect(playlistContenxt, SIGNAL("triggered()"), self.addToPlaylistAction)
self.musicTable.addAction(playlistContenxt)

currentItem.text() is a playlist name thats being fetched from db, as you can see only one function (addToPlaylistAction) receives all triggers from different actions. On my addToPlaylistAction function, how do I determine which menu has been clicked?

+2  A: 

You can use QAction.setData to set some data, so that the slot knows which playlist to add to. Then from the slot you can call self.sender() to get the action that triggered the signal, and use action.data() to get the data back.

Lukáš Lalinský
If you use sender(), just be careful that your function is always called as a slot so that sender() will return something valid.
Kaleb Pederson
+2  A: 

The correct way is to use signal mapper: You can assign data to each of the senders and get a signal with that data.

Bluebird75