Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt transform translate of pixmap having no affect

I need to rotate and translate a QPixmap derived from an image I can transform the pixmap using rotate but translate does not appear to move the image. Can someone suggest changes to the example below to move the image to given x and y values?

To run the code, replace test.png with a convenient small image file.

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtCore import Qt
from PyQt5 import QtCore, QtGui 

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setFixedSize(640, 480)

        label = QLabel("PyQt5 label!")
        label.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(label)

        pixmap = QtGui.QPixmap("test.png")
        label.setPixmap(pixmap)
        xform = QtGui.QTransform().translate(250,50)
        xform.rotate(12) 
        xformed_pixmap = pixmap.transformed(xform, QtCore.Qt.SmoothTransformation)
        label.setPixmap(xformed_pixmap)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
like image 712
Michael M Avatar asked Oct 21 '25 19:10

Michael M


1 Answers

Instead of using QTransforma to move the QPixmap you should move the QLabel:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QTransform
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setFixedSize(640, 480)

        label = QLabel(self)
        label.move(250, 50)

        pixmap = QPixmap("test.png")
        label.setPixmap(pixmap)
        xform = QTransform()
        xform.rotate(12)
        xformed_pixmap = pixmap.transformed(xform, Qt.SmoothTransformation)
        label.setPixmap(xformed_pixmap)
        label.adjustSize()


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
like image 66
eyllanesc Avatar answered Oct 24 '25 08:10

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!