Improve ScreenshotOverlay paintEvent to enhance selection rectangle rendering and dimmed background

This commit is contained in:
2025-07-03 16:33:26 +02:00
parent c89a30da71
commit acfc126bb1
+21 -11
View File
@@ -109,22 +109,32 @@ class ScreenshotOverlay(QtWidgets.QWidget):
def paintEvent(self, event):
qp = QtGui.QPainter(self)
# Draw dimmed background
if self._bg:
if not self._bg:
return
# Draw the background screenshot first.
qp.drawPixmap(0, 0, self._bg)
qp.setBrush(QtGui.QColor(0, 0, 0, 120))
qp.setPen(QtCore.Qt.PenStyle.NoPen)
qp.drawRect(self.geometry())
# Draw selection rectangle
# Prepare a path for the dimmed overlay.
dim_path = QtGui.QPainterPath()
dim_path.addRect(QtCore.QRectF(self.geometry()))
# If a selection is being made, subtract it from the dimmed path.
if self.start and self.end:
sel_rect = QtCore.QRect(self.start, self.end).normalized()
selection_rect = QtCore.QRect(self.start, self.end).normalized()
selection_path = QtGui.QPainterPath()
selection_path.addRect(QtCore.QRectF(selection_rect))
dim_path = dim_path.subtracted(selection_path)
# Draw the border of the selection.
qp.setBrush(QtCore.Qt.BrushStyle.NoBrush)
qp.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 2))
qp.drawRect(sel_rect)
# Optionally, highlight the selected area
qp.setBrush(QtGui.QColor(0, 255, 0, 40))
qp.drawRect(selection_rect)
# Fill the dimmed area (the path that excludes the selection).
qp.setBrush(QtGui.QColor(0, 0, 0, 120))
qp.setPen(QtCore.Qt.PenStyle.NoPen)
qp.drawRect(sel_rect)
qp.drawPath(dim_path)
class MainWindow(QtWidgets.QMainWindow):
def automation_loop(self):