Reputation: 43
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_()
Upvotes: 3
Views: 1211
Reputation: 244301
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_()
Upvotes: 3