Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make transparent cross symbol in python pyqt5

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_())
like image 396
Cascraft Cascraft Avatar asked Jun 11 '20 03:06

Cascraft Cascraft


1 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 :)

like image 121
Sahith Kurapati Avatar answered Oct 12 '22 12:10

Sahith Kurapati