PyQt5 Выделение рамкой

Реализация функционала выделения рамкой


import sys
from PyQt5 import QtWidgets, QtCore, QtChart, QtGui
from PyQt5.QtWidgets import QRubberBand
from PyQt5.QtCore import QRect, Qt

class MyWindow(QtWidgets.QFrame):

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent=parent)
        self.rubber_band = QRubberBand(QRubberBand.Rectangle, self)
        self.origin = None

    def mousePressEvent(self, mouse_event):
        """
        Args:
            mouse_event (QtGui.QMouseEvent): событие мышки
        Returns:
            None
        """
        if mouse_event.button() == Qt.LeftButton:
            self.origin = mouse_event.pos()
            if self.rubber_band.isHidden():
                self.rubber_band.setGeometry(QRect(self.origin, QtCore.QSize()))
                self.rubber_band.show()

    def mouseMoveEvent(self, mouse_event):
        """
        Args:
            mouse_event (QtGui.QMouseEvent): событие мышки
        Returns:
            None
        """
        self.rubber_band.setGeometry(
            QRect(self.origin, mouse_event.pos()).normalized())

    def mouseReleaseEvent(self, mouse_event):
        """
        Args:
            mouse_event (QtGui.QMouseEvent): событие мышки
        Returns:
            None
        """
        if not self.rubber_band.isHidden():
            self.rubber_band.hide()

app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())

Ссылки по теме:

класс QRubberBand, перечисление MouseButton-enum