1.主题思路: 首先加载bin图像数据,然后QPixmap显示图像,最后自定义继承QLabel的子类,实现截图
2.遇到的问题:实现过程中,出现一个问题,使用自定义的label子类,显示的图像很小,不知道为什么要需要将QLael resize 一下,有大神明白的,欢迎留言
mylabel类
from PyQt5.QtWidgets import QLabel
from PyQt5.QtCore import QRect, Qt, QPoint, pyqtSignal, QObject
from PyQt5.QtGui import QPainter, QPen
class SignalObj(QObject):
"""
定义一个信号的类
"""
# 自定义一个信号,注意这个地方定义约束发送出去的参数类型,下面要一致
sendMsg = pyqtSignal()
def __init__(self):
super().__init__()
def run(self):
self.sendMsg.emit()
class MyLabel(QLabel):
def __init__(self,parent=None):
super(MyLabel, self).__init__(parent)
self.ratio = None
self.height = None
self.width = None
self.x0=0
self.y0=0
self.x1=0
self.y1=0
self.startPoint = QPoint()
self.endPoint =QPoint()
self.flag=False
self.move=False # 存在移动
self.isCrossBorder=False
#信号槽
self.cutFinished = SignalObj()
def setWidthAndHeight(self,label_width,label_height,ratio):
self.width = label_width
self.height = label_height
self.resize(self.width,self.height) #重新设定label大小,否则会出现绘制区域很小的bug 原因未知
self.ratio = ratio
# 鼠标移动事件
def mouseMoveEvent(self, event):
# barHeight = self.bar.height()
self.move = True
if self.flag:
self.x1 = event.pos().x()
self.y1 = event.pos().y()
self.update()
if self.x1>self.width or self.y1>self.height:
self.isCrossBorder=True
else:
self.isCrossBorder = False
# 鼠标释放事件
def mouseReleaseEvent(self, event):
self.flag = False
self.move=False
print(self.x0,self.y0,self.x1,self.y1)
self.startPoint = (int(self.x0*self.ratio), int(self.y0*self.ratio))
self.endPoint = (int(self.x1*self.ratio), int(self.y1*self.ratio))
self.cutFinished.run()
self.x0, self.y0, self.x1, self.y1=(0,0,0,0)
print(self.x0, self.y0, self.x1, self.y1)
# 绘制事件
def paintEvent(self, event):
super().paintEvent(event)
if self.flag and self.move and self.x1<= self.width and self.y1<= self.height: # 只有当鼠标按下并且移动状态
rect = QRect(self.x0, self.y0, abs(self.x1 - self.x0), abs(self.y1 - self.y0))
painter = QPainter(self)
painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
painter.drawRect(rect)
# print(self.x0, self.y0, self.x1, self.y1)
# 单击鼠标触发事件
def mousePressEvent(self, event):
# barHeight = self.bar.height()
self.x0 = event.pos().x()
self.y0 = event.pos().y()
self.flag = True
调用
self.mylabel = MyLabel(self.label)
self.mylabel.cutFinished.sendMsg.connect(self.slot_showScreenPic)
3.QPixmap中的setPixmap和drawPixmap使用区别,哪位大师明白,望告知。我使用的过程中,setPixmap后,未使用自定义label类,使用过滤器eventFileter在主界面指向绘图的label,但是画圈只能在图像下层显示,不知如何解决?