Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt context menu

Tags:

python

qt

pyqt4

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?

like image 509
Marconi Avatar asked Aug 05 '26 22:08

Marconi


2 Answers

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

like image 150
Philippe F Avatar answered Aug 08 '26 11:08

Philippe F


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.

like image 26
Lukáš Lalinský Avatar answered Aug 08 '26 11:08

Lukáš Lalinský



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!