重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍Pyqt如何实现无边框窗口拖动以及窗口大小改变,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联是一家专业提供德阳企业网站建设,专注与网站建设、成都网站建设、H5页面制作、小程序制作等业务。10年已为德阳众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。具体内容如下
# coding:utf-8 from PyQt4.Qt import * import sys PADDING=4 sys.setrecursionlimit(10000) class ShadowWidget(QWidget): def __init__(self,parent=None): super(ShadowWidget,self).__init__(parent) self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowSystemMenuHint) self.setAttribute(Qt.WA_TranslucentBackground) self.SHADOW_WIDTH=0 #边框距离 self.isLeftPressDown = False #鼠标左键是否按下 self.dragPosition=0 #拖动时坐标 self.Numbers = self.enum(UP=0, DOWN=1, LEFT=2, RIGHT=3, LEFTTOP=4, LEFTBOTTOM=5, RIGHTBOTTOM=6, RIGHTTOP=7, NONE=8) #枚举参数 self.setMinimumHeight(500) #窗体最小高度 self.setMinimumWidth(1000) #窗体最小宽度 self.dir=self.Numbers.NONE #初始鼠标状态 self.setMouseTracking(True) #绘制边框阴影 def drawShadow(self,painter): #绘制左上角、左下角、右上角、右下角、上、下、左、右边框 self.pixmaps=QStringList() self.pixmaps.append(QString("./img/border/left_top.png")) self.pixmaps.append(QString("./img/border/left_bottom.png")) self.pixmaps.append(QString("./img/border/right_top.png")) self.pixmaps.append(QString("./img/border/right_bottom.png")) self.pixmaps.append(QString("./img/border/top_mid.png")) self.pixmaps.append(QString("./img/border/bottom_mid.png")) self.pixmaps.append(QString("./img/border/left_mid.png")) self.pixmaps.append(QString("./img/border/right_mid.png")) painter.drawPixmap(0, 0, self.SHADOW_WIDTH, self.SHADOW_WIDTH, QPixmap(self.pixmaps[0])) #左上角 painter.drawPixmap(self.width()-self.SHADOW_WIDTH, 0, self.SHADOW_WIDTH, self.SHADOW_WIDTH, QPixmap(self.pixmaps[2])) #右上角 painter.drawPixmap(0,self.height()-self.SHADOW_WIDTH, self.SHADOW_WIDTH, self.SHADOW_WIDTH, QPixmap(self.pixmaps[1])) #左下角 painter.drawPixmap(self.width()-self.SHADOW_WIDTH, self.height()-self.SHADOW_WIDTH, self.SHADOW_WIDTH, self.SHADOW_WIDTH, QPixmap(self.pixmaps[3])) #右下角 painter.drawPixmap(0, self.SHADOW_WIDTH, self.SHADOW_WIDTH, self.height()-2*self.SHADOW_WIDTH, QPixmap(self.pixmaps[6]).scaled(self.SHADOW_WIDTH, self.height()-2*self.SHADOW_WIDTH)) #左 painter.drawPixmap(self.width()-self.SHADOW_WIDTH, self.SHADOW_WIDTH, self.SHADOW_WIDTH, self.height()-2*self.SHADOW_WIDTH, QPixmap(self.pixmaps[7]).scaled(self.SHADOW_WIDTH, self.height()- 2*self.SHADOW_WIDTH)) #右 painter.drawPixmap(self.SHADOW_WIDTH, 0, self.width()-2*self.SHADOW_WIDTH, self.SHADOW_WIDTH, QPixmap(self.pixmaps[4]).scaled(self.width()-2*self.SHADOW_WIDTH, self.SHADOW_WIDTH)) #上 painter.drawPixmap(self.SHADOW_WIDTH, self.height()-self.SHADOW_WIDTH, self.width()-2*self.SHADOW_WIDTH, self.SHADOW_WIDTH, QPixmap(self.pixmaps[5]).scaled(self.width()-2*self.SHADOW_WIDTH, self.SHADOW_WIDTH)) #下 #枚举参数 def enum(self,**enums): return type('Enum', (), enums) def region(self,cursorGlobalPoint): #获取窗体在屏幕上的位置区域,tl为topleft点,rb为rightbottom点 rect = self.rect() tl = self.mapToGlobal(rect.topLeft()) rb = self.mapToGlobal(rect.bottomRight()) x = cursorGlobalPoint.x() y = cursorGlobalPoint.y() if(tl.x() + PADDING >= x and tl.x() <= x and tl.y() + PADDING >= y and tl.y() <= y): #左上角 self.dir = self.Numbers.LEFTTOP self.setCursor(QCursor(Qt.SizeFDiagCursor)) #设置鼠标形状 elif(x >= rb.x() - PADDING and x <= rb.x() and y >= rb.y() - PADDING and y <= rb.y()): #右下角 self.dir = self.Numbers.RIGHTBOTTOM self.setCursor(QCursor(Qt.SizeFDiagCursor)) elif(x <= tl.x() + PADDING and x >= tl.x() and y >= rb.y() - PADDING and y <= rb.y()): #左下角 self.dir = self.Numbers.LEFTBOTTOM self.setCursor(QCursor(Qt.SizeBDiagCursor)) elif(x <= rb.x() and x >= rb.x() - PADDING and y >= tl.y() and y <= tl.y() + PADDING): #右上角 self.dir = self.Numbers.RIGHTTOP self.setCursor(QCursor(Qt.SizeBDiagCursor)) elif(x <= tl.x() + PADDING and x >= tl.x()): #左边 self.dir = self.Numbers.LEFT self.setCursor(QCursor(Qt.SizeHorCursor)) elif( x <= rb.x() and x >= rb.x() - PADDING): #右边 self.dir = self.Numbers.RIGHT self.setCursor(QCursor(Qt.SizeHorCursor)) elif(y >= tl.y() and y <= tl.y() + PADDING): #上边 self.dir = self.Numbers.UP self.setCursor(QCursor(Qt.SizeVerCursor)) elif(y <= rb.y() and y >= rb.y() - PADDING): #下边 self.dir = self.Numbers.DOWN self.setCursor(QCursor(Qt.SizeVerCursor)) else: #默认 self.dir = self.Numbers.NONE self.setCursor(QCursor(Qt.ArrowCursor)) def mouseReleaseEvent(self,event): if(event.button() == Qt.LeftButton): self.isLeftPressDown = False if(self.dir != self.Numbers.NONE): self.releaseMouse() self.setCursor(QCursor(Qt.ArrowCursor)) def mousePressEvent(self,event): if(event.button()==Qt.LeftButton): self.isLeftPressDown=True if(self.dir != self.Numbers.NONE): self.mouseGrabber() else: self.dragPosition = event.globalPos() - self.frameGeometry().topLeft() def mouseMoveEvent(self,event): gloPoint = event.globalPos() rect = self.rect() tl = self.mapToGlobal(rect.topLeft()) rb = self.mapToGlobal(rect.bottomRight()) if(not self.isLeftPressDown): self.region(gloPoint) else: if(self.dir != self.Numbers.NONE): rmove=QRect(tl, rb) if(self.dir==self.Numbers.LEFT): if(rb.x() - gloPoint.x() <= self.minimumWidth()): rmove.setX(tl.x()) else: rmove.setX(gloPoint.x()) elif(self.dir==self.Numbers.RIGHT): print u"youbian" rmove.setWidth(gloPoint.x() - tl.x()) elif(self.dir==self.Numbers.UP): if(rb.y() - gloPoint.y() <= self.minimumHeight()): rmove.setY(tl.y()) else: rmove.setY(gloPoint.y()) elif(self.dir==self.Numbers.DOWN): rmove.setHeight(gloPoint.y() - tl.y()) elif(self.dir==self.Numbers.LEFTTOP): if(rb.x() - gloPoint.x() <= self.minimumWidth()): rmove.setX(tl.x()) else: rmove.setX(gloPoint.x()) if(rb.y() - gloPoint.y() <= self.minimumHeight()): rmove.setY(tl.y()) else: rmove.setY(gloPoint.y()) elif(self.dir==self.Numbers.RIGHTTOP): rmove.setWidth(gloPoint.x() - tl.x()) rmove.setY(gloPoint.y()) elif(self.dir==self.Numbers.LEFTBOTTOM): rmove.setX(gloPoint.x()) rmove.setHeight(gloPoint.y() - tl.y()) elif(self.dir==self.Numbers.RIGHTBOTTOM): rmove.setWidth(gloPoint.x() - tl.x()) rmove.setHeight(gloPoint.y() - tl.y()) else: pass self.setGeometry(rmove) else: self.move(event.globalPos() - self.dragPosition) event.accept() if __name__ == '__main__': app = QApplication(sys.argv) shadow = ShadowWidget() shadow.show() sys.exit(app.exec_())
以上是“Pyqt如何实现无边框窗口拖动以及窗口大小改变”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。