Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pixmap的PyQt变换平移不受影响_Python_Pyqt_Transform_Translate - Fatal编程技术网

Python pixmap的PyQt变换平移不受影响

Python pixmap的PyQt变换平移不受影响,python,pyqt,transform,translate,Python,Pyqt,Transform,Translate,我需要旋转和平移从图像导出的QPixmap 我可以使用“旋转”变换像素贴图,但“平移”似乎不会移动图像。 是否有人可以建议对下面的示例进行更改,以将图像移动到给定的x和y值 要运行代码,请将test.png替换为一个方便的小图像文件 import sys from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow from PyQt5.QtCore import Qt from PyQt5 import QtCore, QtGui

我需要旋转和平移从图像导出的QPixmap 我可以使用“旋转”变换像素贴图,但“平移”似乎不会移动图像。 是否有人可以建议对下面的示例进行更改,以将图像移动到给定的x和y值

要运行代码,请将test.png替换为一个方便的小图像文件

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_()

不应使用QTransforma移动QPixmap,而应移动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_()

谢谢,您的回答解决了我提出的问题,我很好奇您为什么添加adjustSize调用,如果pixmap从未缩放,是否需要此调用。@MichaelM使用adjustSize以便QLabel采用QPixmap的大小,默认情况下QLabel的大小不考虑内容。请阅读和