PyQt不规则形状的窗户(例如没有边框/装饰的圆形)

Nat*_*han 5 python pyqt

如何在PyQt中创建不规则形状的窗口?

我找到了这个C++解决方案,但我不确定如何在Python中这样做.

Fre*_*nan 8

干得好:

from PyQt4 import QtGui, QtWebKit
from PyQt4.QtCore import Qt, QSize

class RoundWindow(QtWebKit.QWebView):
    def __init__(self):
        super(RoundWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

    def sizeHint(self):
        return QSize(300,300)

    def paintEvent(self, event):
        qp = QtGui.QPainter()
        qp.begin(self)
        qp.setRenderHint(QtGui.QPainter.Antialiasing);
        qp.setPen(Qt.NoPen);
        qp.setBrush(QtGui.QColor(255, 0, 0, 127));
        qp.drawEllipse(0, 0, 300, 300);
        qp.end()

a = QtGui.QApplication([])
rw = RoundWindow()
rw.show()
a.exec_()
Run Code Online (Sandbox Code Playgroud)

截图

我从来没有在我的生活中写过C++,但阅读那段代码并不难.您会发现大多数在线Qt文档都是使用C++编写的,因此至少能够阅读它是有用的.