Skip to content

Instantly share code, notes, and snippets.

@StalkingKillah
Created November 4, 2018 21:12
Show Gist options
  • Save StalkingKillah/c886e9a321b8bb0572f40eb031c09bd4 to your computer and use it in GitHub Desktop.
Save StalkingKillah/c886e9a321b8bb0572f40eb031c09bd4 to your computer and use it in GitHub Desktop.
Transparent QMainWindow
from typing import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QTransparentMainWindow(QMainWindow):
closing = pyqtSignal()
_movable = False
_move_offset = 0
_backgroundOpacity = 1.0
_backgroundColor = Qt.gray
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_NoSystemBackground)
def paintEvent(self, event: QPaintEvent):
painter = QPainter(self)
painter.setOpacity(self._backgroundOpacity)
painter.setBrush(self._backgroundColor)
painter.setPen(QPen(self._backgroundColor))
painter.drawRect(self.rect())
def mouseMoveEvent(self, event: QMouseEvent):
if self._movable:
self.move(event.globalX() - self._move_offset.x(), event.globalY() - self._move_offset.y())
def mousePressEvent(self, event: QMouseEvent):
if self._movable:
self._move_offset = event.pos()
def closeEvent(self, event: QCloseEvent):
super().closeEvent(event)
self.closing.emit()
def setBackgroundOpacity(self, opacity: float):
self._backgroundOpacity = opacity
def backgroundOpacity(self) -> float:
return self._backgroundOpacity
def setBackgroundColor(self, color: Union[QBrush, QColor, Qt.GlobalColor, QGradient, Qt.BrushStyle]):
self._backgroundColor = color
def backgroundColor(self) -> Union[QBrush, QColor, Qt.GlobalColor, QGradient, Qt.BrushStyle]:
return self._backgroundColor
def setMovable(self, movable: bool):
self._movable = movable
def movable(self) -> bool:
return self._movable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment