How to make transparent cross symbol in python pyqt5

Question:

Does anyone know how I can make my crosshair transparent or have an opacity?

im trying to make a crosshair that looks like this:

enter image description here

here is the code:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Crosshair(QtWidgets.QWidget):
    def __init__(self, parent=None, windowSize=24, penWidth=2):
        QtWidgets.QWidget.__init__(self, parent)
        self.ws = windowSize
        self.resize(windowSize+1, windowSize+1)
        self.pen = QtGui.QPen(QtGui.QColor(0,255,0,255))
        self.pen.setWidth(penWidth)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.WindowTransparentForInput)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
        self.move(QtWidgets.QApplication.desktop().screen().rect().center() - self.rect().center() + QtCore.QPoint(1,1))


    def paintEvent(self, event):
        ws = self.ws
        d = 241
        painter = QtGui.QPainter(self)
        painter.setPen(self.pen)
        # painter.drawLine( x1,y1, x2,y2    )
        painter.drawLine(ws/2, 0,               ws/2, ws/2 - ws/d)   # Top
        painter.drawLine(ws/2, ws/2 + ws/d,     ws/2, ws)   # Bottom
        painter.drawLine(0, ws/2,               ws/2 - ws/d, ws/2)   # Left
        painter.drawLine(ws/2 + ws/d, ws/2,     ws, ws/2)   # Right


app = QtWidgets.QApplication(sys.argv)

widget = Crosshair(windowSize=241, penWidth=2.5)
widget.show()

sys.exit(app.exec_())
Asked By: Cascraft Cascraft

||

Answers:

You can use the setOpacity() function present in QPainter.

You can see the documentation here

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Crosshair(QtWidgets.QWidget):
    def __init__(self, parent=None, windowSize=24, penWidth=2, opacity=1):
        QtWidgets.QWidget.__init__(self, parent)
        self.ws = windowSize
        self.resize(windowSize+1, windowSize+1)
        self.pen = QtGui.QPen(QtGui.QColor(0,255,0,255))
        self.pen.setWidth(penWidth)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.WindowTransparentForInput)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
        self.move(QtWidgets.QApplication.desktop().screen().rect().center() - self.rect().center() + QtCore.QPoint(1,1))
        self.opacity = opacity


    def paintEvent(self, event):
        ws = self.ws
        d = 241
        painter = QtGui.QPainter(self)
        painter.setOpacity(self.opacity)
        painter.setPen(self.pen)
        # painter.drawLine( x1,y1, x2,y2    )
        painter.drawLine(ws/2, 0,               ws/2, ws/2 - ws/d)   # Top
        painter.drawLine(ws/2, ws/2 + ws/d,     ws/2, ws)   # Bottom
        painter.drawLine(0, ws/2,               ws/2 - ws/d, ws/2)   # Left
        painter.drawLine(ws/2 + ws/d, ws/2,     ws, ws/2)   # Right


app = QtWidgets.QApplication(sys.argv)

widget = Crosshair(windowSize=241, penWidth=2.5, opacity=0.5)
widget.show()

sys.exit(app.exec_())

Hope this helps 🙂

Answered By: Sahith Kurapati
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.