I wrote a SnippingWidget function and linked it to the main code. After executing this function, my application crashes with an error. I looked up what the error is with try and it says: "PyQt5.QtCore.pyqtSignal' object has no attribute 'emit'". I want this function to just hide itself when finished, meaning it can be reused. Initially the same error came up, but with "closed", I was able to solve it. But I don't understand what to do with emit....
Here is the code for my function:
from PyQt5 import QtCore, QtGui, QtWidgets
class SnippingWidget(QtWidgets.QDialog):
closed = QtCore.pyqtSignal()
def __init__(self):
super(SnippingWidget, self).__init__()
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.setStyleSheet("background:transparent;")
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.outsideSquareColor = "red"
self.squareThickness = 2
self.start_point = QtCore.QPoint()
self.end_point = QtCore.QPoint()
self.selected_area = None
# Установка размеров окна в соответствии с размерами экрана
screen_geometry = QtWidgets.QApplication.desktop().screenGeometry()
self.setGeometry(screen_geometry)
def mousePressEvent(self, event):
self.start_point = event.pos()
self.end_point = event.pos()
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.CrossCursor)
self.update()
def mouseMoveEvent(self, event):
self.end_point = event.pos()
self.update()
def mouseReleaseEvent(self, QMouseEvent):
r = QtCore.QRect(self.start_point, self.end_point).normalized()
self.hide()
self.selected_area = r
QtWidgets.QApplication.restoreOverrideCursor()
self.closed.emit()
self.start_point = QtCore.QPoint()
self.end_point = QtCore.QPoint()
def paintEvent(self, event):
trans = QtGui.QColor(22, 100, 233)
r = QtCore.QRectF(self.start_point, self.end_point).normalized()
qp = QtGui.QPainter(self)
trans.setAlphaF(0.2)
qp.setBrush(trans)
outer = QtGui.QPainterPath()
outer.addRect(QtCore.QRectF(self.rect()))
inner = QtGui.QPainterPath()
inner.addRect(r)
r_path = outer - inner
qp.drawPath(r_path)
qp.setPen(
QtGui.QPen(QtGui.QColor(self.outsideSquareColor), self.squareThickness)
)
trans.setAlphaF(0)
qp.setBrush(trans)
qp.drawRect(r)
For "closed", I defined a closed signal:
closed = QtCore.pyqtSignal()