如何围绕QWidget角落

时间:2016-02-11 07:32:08

标签: python qt pyqt pyside

enter image description here

我想知道是否有办法绕过Qt小部件角落?

from PyQt4 import QtCore, QtGui

class Custom(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self.setWindowOpacity(0.9)
        self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)
        self.setWindowTitle('Custom')
        self.resize(440,220)
        self.move(QtGui.QCursor.pos())

    def closeEvent(self, event):
        event.accept()
        sys.exit(app.exec_())

    def mousePressEvent(self, event):
        self.close() 

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Custom()
    w.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:5)

您可以使用QWidget.setMask(self, QRegion)

C ++中的一个例子:

QWidget *widget = new QWidget;
widget->resize(300, 200);

const int radius = 10;

QPainterPath path;
path.addRoundedRect(widget->rect(), radius, radius);
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
widget->setMask(mask);

widget->show();

答案 1 :(得分:2)

SOLUTION:

enter image description here

以下是hank使用C ++概述的有效Python解决方案:

import sys
from PySide import QtCore, QtGui

class Custom(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self.setWindowOpacity(0.9)
        self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)
        self.setWindowTitle('Custom')

        radius = 40.0
        path = QtGui.QPainterPath()
        self.resize(440,220)
        path.addRoundedRect(QtCore.QRectF(self.rect()), radius, radius)
        mask = QtGui.QRegion(path.toFillPolygon().toPolygon())
        self.setMask(mask)
        self.move(QtGui.QCursor.pos())

    def closeEvent(self, event):
        event.accept()
        sys.exit(app.exec_())

    def mousePressEvent(self, event):
        self.close() 

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Custom()
    w.show()
    sys.exit(app.exec_())