Last active
October 26, 2021 06:56
-
-
Save PaulleDemon/2b312275e34db3932033fe955732cfd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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