qpixmap 鼠标移动,局部放大

// void CMyLabel::mouseMoveEvent ( QMouseEvent * event )
// {
// if (m_bMoveMap)
// {
//   m_MapRect.setWidth(40);
//   m_MapRect.setHeight(40);
// m_MapRect.setX(event->x());
// m_MapRect.setY(event->y());
// 
// m_pixmap = QPixmap::grabWindow(winId(), m_MapRect.x()-10, m_MapRect.y()-10, 20, 20);
// 
// m_pixmap = m_pixmap.scaled(m_MapRect.width(),m_MapRect.height(),Qt::KeepAspectRatio);
// 
// nMapX = event->x() - 20;
// nMapY = event->y() - 20;
// 
// update();
// }

// }



if (m_bMap || m_bMoveMap)
{
painter.drawPixmap(nMapX, nMapY, m_pixmap);
}

以下是使用 PyQt 编写的随鼠标移动放大镜示例: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout from PyQt5.QtGui import QPixmap, QCursor, QPainter, QPen, QBrush from PyQt5.QtCore import Qt, QPoint class MagnifyingGlass(QWidget): def __init__(self): super().__init__() self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool) self.setAttribute(Qt.WA_TranslucentBackground) self.label = QLabel(self) self.label.setScaledContents(True) self.pix = QPixmap('image.jpg') self.label.setPixmap(self.pix) self.magnify = 2.0 self.resize(self.pix.width() // self.magnify, self.pix.height() // self.magnify) self.setMouseTracking(True) def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QPen(Qt.red, 1)) painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.drawEllipse(self.width() // 2 - 5, self.height() // 2 - 5, 10, 10) def mouseMoveEvent(self, event): cursor_pos = QCursor.pos() x = cursor_pos.x() - self.width() // 2 y = cursor_pos.y() - self.height() // 2 if x < 0: x = 0 elif x > self.pix.width() - self.width() // self.magnify: x = self.pix.width() - self.width() // self.magnify if y < 0: y = 0 elif y > self.pix.height() - self.height() // self.magnify: y = self.pix.height() - self.height() // self.magnify self.label.setPixmap(self.pix.copy(x * self.magnify, y * self.magnify, self.width() * self.magnify, self.height() * self.magnify)) self.move(cursor_pos.x() - self.width() // 2, cursor_pos.y() - self.height() // 2) def resizeEvent(self, event): self.label.resize(self.size()) if __name__ == '__main__': app = QApplication(sys.argv) magnify = MagnifyingGlass() magnify.show() sys.exit(app.exec_()) ``` 该示例中,我们使用了 `QLabel` 来显示要放大的图片,使用 `QPainter` 绘制了放大镜的圆形边框,并且使用 `setMouseTracking(True)` 方法来跟踪鼠标移动。当鼠标移动时,我们获取到鼠标的位置 `cursor_pos`,然后根据放大倍数 `magnify` 计算出要显示的区域,并将其设置为 `QLabel` 的 pixmap。最后,使用 `move()` 方法将放大移动鼠标所在的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值