码迷,mamicode.com
首页 > 其他好文 > 详细

PyQt5 无边框窗口重新定义鼠标事件

时间:2020-05-01 20:55:07      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:center   color   res   on()   common   get   src   with   flags   

PyQt5 无边框窗口重新定义鼠标事件

 

#! /usr/bin/env python

# -*- coding:utf-8 -*-

 

import sys

from PyQt5.QtWidgets import QApplication, QWidget

from PyQt5.QtCore import Qt 

from PyQt5.QtGui import QCursor

 

class NoBorderWindow(QWidget):

    def __init__(self):

        super().__init__()

        self.window_UI()

        self.qss()

 

    def window_UI(self):

        self.resize(600, 200) 

        self.setWindowFlags(Qt.FramelessWindowHint)

 

    def qss(self):

        self.qssfile = "./qss/noborder.qss"

        self.style = CommonStyleSheet.loadqss(self.qssfile)

        self.setStyleSheet(self.style)

 

    # 重新定义鼠标事件

    def mousePressEvent(self, QMouseEvent):

        if QMouseEvent.button() == Qt.LeftButton:

            self.drag = True

            self.dragPosition = QMouseEvent.globalPos() - self.pos()

            QMouseEvent.accept()

            self.setCursor(QCursor(Qt.PointingHandCursor))

 

    def mouseMoveEvent(self, QMouseEvent):

        if Qt.LeftButton and self.drag:

            self.move(QMouseEvent.globalPos() - self.dragPosition)

            QMouseEvent.accept()

 

    def mouseReleaseEvent(self, QMouseEvent):

        self.drag = False

        QMouseEvent.accept()

        self.setCursor(QCursor(Qt.ArrowCursor))

 

class CommonStyleSheet:

    def __init__(self):

        pass

 

    @staticmethod

    def loadqss(style):

        with open (style, "r", encoding="utf-8") as f:

            return f.read()

 

if __name__ == "__main__":

    app = QApplication(sys.argv)

    win = NoBorderWindow()

    win.show()

    sys.exit(app.exec_())

 

 

Qss文件:

 

#w{background-image:url(./images/bg.gif);}

QWidget{background-color: greenyellow;}

 

 技术图片

PyQt5 无边框窗口重新定义鼠标事件

标签:center   color   res   on()   common   get   src   with   flags   

原文地址:https://www.cnblogs.com/tylerwu/p/12814447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!