栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Image Viewer GUI无法正确映射鼠标按下事件的坐标

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Image Viewer GUI无法正确映射鼠标按下事件的坐标

在这些情况下,QRubberBand最好是QLabel的儿子,因此不需要进行很多转换。

另一方面,事件的坐标与窗口相关,因此我们必须将其转换为QLabel的坐标。为此,一种简单的方法是将相对于窗口的局部坐标转换为全局坐标,然后将相对于QLabel的全局坐标转换为局部坐标。

最后,在缩放图像时,由于currentQRect相对于缩放后的QLabel相对,但内部QPixmap尚未缩放,因此会影响坐标。

def mousePressEvent (self, event):    self.originQPoint = self.imageLabel.mapFromGlobal(self.mapToGlobal(event.pos()))     self.currentQRubberBand = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self.imageLabel)    self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, QtCore.QSize()))    self.currentQRubberBand.show()def mouseMoveEvent (self, event):    p = self.imageLabel.mapFromGlobal(self.mapToGlobal(event.pos()))    QtWidgets.QToolTip.showText(event.pos() , "X: {} Y: {}".format(p.x(), p.y()), self)    if self.currentQRubberBand.isVisible() and self.imageLabel.pixmap() is not None:        self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, p).normalized() & self.imageLabel.rect())def mouseReleaseEvent (self, event):    self.currentQRubberBand.hide()    currentQRect = self.currentQRubberBand.geometry()    self.currentQRubberBand.deleteLater()    if self.imageLabel.pixmap() is not None:        tr = QtGui.QTransform()        if self.fitToWindowAct.isChecked(): tr.scale(self.imageLabel.pixmap().width()/self.scrollArea.width(),      self.imageLabel.pixmap().height()/self.scrollArea.height())        else: tr.scale(1/self.scaleFactor, 1/self.scaleFactor)        r = tr.mapRect(currentQRect)        cropQPixmap = self.imageLabel.pixmap().copy(r)        cropQPixmap.save('output.png')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号