python - 如何在 PyQt5 中使用 slider 旋转图像,同时保持图像居中?

标签 python user-interface pyqt5 geometry rotation

我想使用 PyQt5 将图像 (dial.png) 旋转一度,而不将其从中心位置移动。然而,尽管我尽了最大努力,图像并没有保持在中心位置,并且在旋转时会困惑地移动。每次 QSlider 值改变时,图像应旋转一度。

下面是我当前用于创建包含要旋转的图像的标签的代码:

        self.dial = QtWidgets.QLabel(self.centralwidget)
        self.dial.setGeometry(QtCore.QRect(150, 50, 500, 440))
        self.dial.setText("")
        self.dial.setObjectName("dial")
        self.pixmap = QtGui.QPixmap("dial.png")
        self.dial.setPixmap(self.pixmap)

我一直无法找到满足我要求的解决方案。任何想法或起点将不胜感激。

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(920, 620)
        MainWindow.setMinimumSize(QtCore.QSize(920, 620))
        MainWindow.setMaximumSize(QtCore.QSize(920, 620))

这是dial.png:

dial.png

最佳答案

我不确定你的代码是否正确,但即使是正确的,你提到的困惑运动仍然存在,因为 PNG 图像没有正确居中,如下所示:

cp

当然,计算机程序会利用图像的实际中心,而不是人类逻辑的中心。

话虽这么说,这是完成您所要求的一种可能的方法,包括足够的注释来帮助您理解逻辑:

from PyQt5.QtGui import QPixmap, QTransform
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QSlider, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt

# Define a class that inherits from QMainWindow
class ImageRotator(QMainWindow):
    def __init__(self, image_path):
        # Call the __init__ method of the QMainWindow class to initialize the basic functionality of the main window
        super().__init__()

        # Load the image from the given image path
        self.image = QPixmap(image_path)

        # Create a QLabel to display the image
        self.label = QLabel()
        self.label.setPixmap(self.image)
        # Set the alignment of the label to center
        self.label.setAlignment(Qt.AlignCenter)

        # Create a QSlider to control the rotation
        self.slider = QSlider(Qt.Horizontal)
        # Set the minimum and maximum values of the slider to 0 and 360 degrees, respectively
        self.slider.setMinimum(0)
        self.slider.setMaximum(360)
        # Set the initial value of the slider to 0 degrees
        self.slider.setValue(0)

        # Connect the valueChanged signal of the slider to the rotate_image method
        self.slider.valueChanged.connect(self.rotate_image)

        # Create a QVBoxLayout to hold the QLabel and QSlider
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.slider)

        # Create a QWidget to hold the QVBoxLayout
        widget = QWidget()
        widget.setLayout(layout)
        # Set the QWidget as the central widget of the QMainWindow
        self.setCentralWidget(widget)

        # Show the QMainWindow
        self.show()

    def rotate_image(self, angle):
        # Create a QTransform object that will rotate the image around its center point by the given angle
        transform = QTransform().rotate(angle)
        # Apply the transformation to the image and create a new QPixmap object
        rotated_image = self.image.transformed(transform, Qt.SmoothTransformation)
        # Set the rotated image as the pixmap of the QLabel
        self.label.setPixmap(rotated_image)

# Create a QApplication object
app = QApplication([])
# Create an instance of the ImageRotator class with the "dial.png" image file
window = ImageRotator("dial.png")
# Start the QApplication
app.exec_()

关于python - 如何在 PyQt5 中使用 slider 旋转图像,同时保持图像居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75635358/

相关文章:

python - 为什么 mouseMoveEvent 在 PyQt5 中什么都不做

python - 通过 session 将两个模块路由到 Flask 中的同一个 URL

python - 如何将 'google.cloud.documentai.v1beta2.types.document.Document' 转换为 JSON?

android - 如何显示数字时钟小部件的秒数?

android - 布局预览与模拟器或手机不匹配

objective-c - NSBox 对焦环?

python - pyqtgraph 轴显示不正确

python - 在 Python 2.5 中用微秒解析日期时间字符串

python - PySide/PyQt : PointingHandCursor recipes?

python - 放在 QMainWindow 的 Central Widget 中的 QTabWidget 拉伸(stretch)太多,覆盖了菜单栏