简体   繁体   中英

How to scale and move an image Qlabel Pyqt5

I'm having troubled scaling and moving an image at the same time. The image is setting in a QLabel.

UPDATE

I'm handling the movement with mouseMoveEvent

    def mousePressEvent(self, evt):
        self.oldPos = evt.globalPos()

    def mouseMoveEvent(self, evt):
        self._moveFrameImage(evt, self.oldPos)
        self.oldPos = evt.globalPos()

    def _moveFrameImage(self, evt, oldPos):
        delta = QtCore.QPoint(evt.globalPos() - oldPos)
        self.initPointImageSatelital = 
        QtCore.QPoint(self.lbl_imagenSatelital.x() + delta.x() , 
        self.lbl_imagenSatelital.y() + delta.y())
        self.lbl_imagenSatelital.move(self.lbl_imagenSatelital.x() + 
        delta.x(), self.lbl_imagenSatelital.y() + delta.y())
        self.lbl_imagenSatelital.move(self.initPointImageSatelital)
        self.oldPos = evt.globalPos()

For the scaling, I'm using the wheelEvent

    def wheelEvent(self,event):

        valueDelta = event.angleDelta().y()
        self._zoomInOut(valueDelta)
        self.lbl_imagenSatelital.move(-300,-100)

    def _zoomInOut(self, deltaIncrease: int):
        if deltaIncrease > 0:
            self.zoom_out()
            self.zoomValue = self.zoomValue + 5
        else:
            self.zoom_in()
            self.zoomValue = self.zoomValue - 5     
    
    def zoom_in(self):
        self.widthImagenSatelital += 10
        self.heightImagenSatelital += 10
        self.resize_image()

    def zoom_out(self):
        self.widthImagenSatelital -= 10
        self.heightImagenSatelital -= 10
        self.resize_image()

    def resize_image(self):
        self.lbl_imagenSatelital.move(self.initPointImageSatelital)
        
        self.lbl_imagenSatelital.setPixmap(self.pixImage.scaled(
            self.widthImagenSatelital, self.heightImagenSatelital, QtCore.Qt.KeepAspectRatio,
            QtCore.Qt.SmoothTransformation))

        self.lbl_imagenSatelital.move(self.initPointImageSatelital)

The parent of my Qlabel is the QMainWindow

    def __init__(self):
        super(introWindow, self).__init__()
        self.centralwidget = QtWidgets.QWidget(self)
        self._insertImageAtFrame()

    def _insertImageAtFrame(self):
      
        fileNamePath = 'C:/Users/Investigaciones-Rob/Desktop/imagen_satelital.png'
        
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) #Minimum y Prefered  

        self.pixImage = QtGui.QPixmap(fileNamePath)

        self.lbl_imagenSatelital = QtWidgets.QLabel()
        self.lbl_imagenSatelital.setParent(self)
        self.heightImagenSatelital = 400
        self.widthImagenSatelital = 600
        self.zoomValue = 0
        
        self.lbl_imagenSatelital.setPixmap(self.pixImage.scaled(self.widthImagenSatelital, self.heightImagenSatelital, QtCore.Qt.KeepAspectRatio))            
        self.lbl_imagenSatelital.setSizePolicy(sizePolicy)

The problem is after I used setPixmap in self.resize_image(), the image move to the origin of the window. It seems that the move function is not working.

在此处输入图像描述

after scaling:

在此处输入图像描述

I found the mistake. Just erase this line

self.lbl_imagenSatelital.move(-300,-100)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM