animatedtiles.py
总结:这个小程序,主要涉及到控件的布局(boundingRect Method),形状的绘制(shape函数重载来自定义控件的形状),控件图片的加载(资源文件的引入pyrcc,Button对象将资源对象最为初始化参数),整个界面的大体布局(scene view graphicsitem 各种qt对象之间的依赖关系),动态绘图过程(QState状态机)
注释即是笔记,留作下次的备忘
from PyQt5.QtCore import (pyqtProperty, pyqtSignal, QEasingCurve, QObject,
QParallelAnimationGroup, QPointF, QPropertyAnimation, qrand, QRectF,
QState, QStateMachine, Qt, QTimer)
from PyQt5.QtGui import (QBrush, QLinearGradient, QPainter, QPainterPath,
QPixmap)
from PyQt5.QtWidgets import (QApplication, QGraphicsItem, QGraphicsPixmapItem,
QGraphicsRectItem, QGraphicsScene, QGraphicsView, QGraphicsWidget,
QStyle)
import animatedtiles_rc
# PyQt doesn't support deriving from more than one wrapped class so we use
# composition and delegate the property.
class Pixmap(QObject):
def __init__(self, pix):
#父类初始化
super(Pixmap, self).__init__()
self.pixmap_item = QGraphicsPixmapItem(pix)
self.pixmap_item.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
def _set_pos(self, pos):
self.pixmap_item.setPos(pos)
# 类似于C# 字段调用方法 python里面property函数
pos = pyqtProperty(QPointF, fset=_set_pos)
class Button(QGraphicsWidget):
pressed = pyqtSignal()
def __init__(self, pixmap, parent_arg=None):
#QGraphicsWidget :_init__ (self, QGraphicsItem parent = None, Qt.WindowFlags wFlags = 0)
#The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene
super(Button, self).__init__(parent_arg)
self._pix = pixmap
#设置悬停时间是否接受,系统一直跟随鼠标检测是否悬停在一个控件上
self.setAcceptHoverEvents(True)