1

What is the way of getting the Sender of a right-mouse-click on a QLabel()? I want to know on which Widget the right-mouse-click happened. I have code to get the position, but how can i get the Sender?

Getting the Sender i could retrieve the accessibleName()

Here is my current minimal Code:

#!/usr/bin/python
# -*- coding: utf-8 -*-



import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *    #!/usr/bin/python
# -*- coding: utf-8 -*-



import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):               

        # qbtn = QtGui.QPushButton('Quit', self)
        label =  QLabel("BG Sessions", self)
        label.setContextMenuPolicy(Qt.CustomContextMenu)
        label.setObjectName("title")
        label.customContextMenuRequested.connect(self.clearCache)
        label.move(50, 50)       

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Quit button')    
        self.show()

    def clearCache(self, pos):
        print pos


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
user1767754
  • 23,311
  • 18
  • 141
  • 164

3 Answers3

1

if you want to use sender() as suggested by Alexander Lutsenko keep this in mind: QT-documentation

Warning: This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.

if all widgets sending the signal are from the same type,findChild() can be used instead. this is working with my tableViews:

# create signalMapper
self.signalMapper = QtCore.QSignalMapper(self)
self.signalMapper.mapped[str].connect(<slot>)

# connect the widgets with signalMapper
self.<widget>.<yourSignal>.connect(self.signalMapper.map)
self.signalMapper.setMapping(self.<widget>, self.<widget>.objectName())         # sends objectName() to slot

# in slot:
obj = self.findChild(QtWidgets.QLabel,objectname)
a_manthey_67
  • 4,046
  • 1
  • 17
  • 28
1

You could change the way you connect your signal such that you pass in a reference to the object you are connecting the signal from.

For instance:

my_object.customContextMenuRequested.connect(lambda pos, obj=my_object: self.clearCache(pos, obj))

(you will of course need to modify clearCache() so that it accepts this extra argument)

Note: the obj=my_object line is not redundant, see here for details.

Community
  • 1
  • 1
three_pineapples
  • 11,579
  • 5
  • 38
  • 75
  • what when i just want to pas obj but not pos? When i am doing that, i am still getting the Position of the object – user1767754 Sep 08 '15 at 18:47
  • The `customContextMenuRequested` signal emits the position, and there is nothing you can do about that. So while you still have to has `pos` as an argument to the `lambda` function, you don't have to pass it to `clearCache` if you don't want to (for example `my_object.customContextMenuRequested.connect(lambda pos, obj=my_object: self.clearCache(obj))` – three_pineapples Sep 08 '15 at 23:28
0

self.sender() inside the slot does this job.

Alexander Lutsenko
  • 2,130
  • 8
  • 14