Skip to content

Instantly share code, notes, and snippets.

@PaulleDemon
Last active October 26, 2021 06:56
Show Gist options
  • Select an option

  • Save PaulleDemon/2b312275e34db3932033fe955732cfd7 to your computer and use it in GitHub Desktop.

Select an option

Save PaulleDemon/2b312275e34db3932033fe955732cfd7 to your computer and use it in GitHub Desktop.
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
class ClickableLabel(QtWidgets.QLabel):
# you will have to specify how many arguments and the type of the argument(eg: bool, int, str), in
# our case we will use QPoint to pass mouse position.
# All your signals must be class level, not object level/instance level.
clicked = QtCore.pyqtSignal(QtCore.QPoint)
def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
super(ClickableLabel, self).mousePressEvent(event)
self.clicked.emit(event.pos()) # we will emit the signal and pass the position
def print_label_click_pos(pos):
print(pos)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
label = ClickableLabel("Sample label")
label.clicked.connect(print_label_click_pos) # connect the signal to a slot
label.show()
sys.exit(app.exec())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment