PyQt边缘透阴影明窗口实现

实现类似QQ的无边框窗口界面的边缘效果
PyQt边缘透阴影明窗口实现_第1张图片
主要有几点:
第一,窗口要设置称无边框:
self.setWindowFlags(Qt.FramelessWindowHint)
第二,窗口要设置成透明:
self.setAttribute(Qt.WA_TranslucentBackground,True)
第三,就是主要实现方法了,主要是在paintEvent方法中进行绘制实现的:
def paintEvent(self,event):
    m = 9
    path = QPainterPath()
    path.setFillRule(Qt.WindingFill)
    path.addRect(m, m, self.width()-m*2, self.height()-m*2)
    painter = QPainter(self)
    #painter.setRenderHint(QPainter.Antialiasing, True)
    painter.fillPath(path, QBrush(Qt.white))
 
    color = QColor(100, 100, 100, 30)
    #for(int i=0; i<10; i++)
    
    for i in range(m):
        path = QPainterPath()
        path.setFillRule(Qt.WindingFill)
        path.addRoundRect(m-i, m-i, self.width()-(m-i)*2, self.height()-(m-i)*2,1,1)
        color.setAlpha(90 - math.sqrt(i)*30)
        painter.setPen(QPen(color,1,Qt.SolidLine))
        painter.drawRoundRect(QRect(m-i, m-i, self.width()-(m-i)*2, self.height()-(m-i)*2), 0,0)

        
可以修改边缘效果的宽度:m的值,可以看到这个方法是先先将除了边缘m宽度的整个窗口填充为白色(可以自定义),然后利用一个for循环,在外围绘制了m条线,而且由内到外逐渐变得透明。


下面贴上源代码(可以直接运行),供小伙伴们参考和改进。

#coding:utf-8
'''
Created on 2015��8��1��

@author: guowu
'''
from PyQt4.QtGui import QWidget, QApplication, QPainterPath, QPainter, QBrush,\
    QColor, QPen
from PyQt4.QtCore import Qt, QSize, QRect, QPoint
import sys
import math
class TestFrame(QWidget):
    def __init__(self,parent=None):
        super(TestFrame,self).__init__(parent)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground,True)
        self.setFixedSize(QSize(500,500))
        self.currentPos = None
    
    def mousePressEvent(self,event):
        self.currentPos = event.pos()
    
    def mouseMoveEvent(self,event):
        self.move(QPoint(self.pos()+event.pos()-self.currentPos))
        
    def paintEvent(self,event):
        m = 9
        
        path = QPainterPath()
        path.setFillRule(Qt.WindingFill)
        path.addRect(m, m, self.width()-m*2, self.height()-m*2)
        
        painter = QPainter(self)
        #painter.drawLine(QLineF)
        #painter.setRenderHint(QPainter.Antialiasing, True)
        painter.fillPath(path, QBrush(Qt.white))
     
        color = QColor(100, 100, 100, 30)
        #for(int i=0; i<10; i++)
        
        for i in range(m):
            path = QPainterPath()
            path.setFillRule(Qt.WindingFill)
            path.addRoundRect(m-i, m-i, self.width()-(m-i)*2, self.height()-(m-i)*2,1,1)
            color.setAlpha(90 - math.sqrt(i)*30)
            painter.setPen(QPen(color,1,Qt.SolidLine))
            painter.drawRoundRect(QRect(m-i, m-i, self.width()-(m-i)*2, self.height()-(m-i)*2), 0,0)
           
#             path = QPainterPath()
#             path.setFillRule(Qt.WindingFill)
#             path.addRect(m-i, m-i, self.width()-(m-i)*2, self.height()-(m-i)*2)
#             color.setAlpha(90 - math.sqrt(i)*30)
#             painter.setPen(QPen(color,1))
#             painter.drawPath(path)
         
    
    
    

if __name__ == "__main__":
    app = QApplication(sys.argv)
    c = TestFrame()
    c.show()
    app.exec_()



你可能感兴趣的:(PyQt)