Marconi
Marconi

Reputation: 3651

PyQt context menu

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?

Upvotes: 3

Views: 2342

Answers (2)

Philippe F
Philippe F

Reputation: 12175

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

Upvotes: 5

Lukáš Lalinský
Lukáš Lalinský

Reputation: 41316

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.

Upvotes: 3

Related Questions