QPixmap:如何在addPixmap中增加图片的尺寸?

QPixmap: How to increase the size of the picture in addPixmap?

class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super(MyGraphicsView, self).__init__()
        scene = QGraphicsScene(self)
        self.tic_tac_toe = TicTacToe()
        scene.addItem(self.tic_tac_toe)

        self.m = QPixmap("exit.png")

        scene.addPixmap(self.m)

        self.setScene(scene)
        self.setCacheMode(QGraphicsView.CacheBackground)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)

png 已经存在。在滚动条中显示在屏幕上的同时增加其大小的方法是什么?

目标是有一个按钮,单击该按钮该图片的大小会增加。

你必须使用 setScale()。此外,当您使用 addPixmap() this returns 时,创建的 QGraphicsPixmapItem.

此外,缩放是一种变换,所以它有一个变换原点,默认情况下它是(0, 0),但在这种情况下,更好的选择是将它放在图像的中心。

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super(MyGraphicsView, self).__init__()
        scene = QGraphicsScene(self)
        self.m = QPixmap("exit.png")
        self.item = scene.addPixmap(self.m)

        self.item.setTransformOriginPoint(self.item.boundingRect().center())

        self.setScene(scene)
        self.setCacheMode(QGraphicsView.CacheBackground)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)

    @pyqtSlot()
    def scale_pixmap(self):
        self.item.setScale(2*self.item.scale())

class Example(QMainWindow):
    def __init__(self):
        super(Example, self).__init__()
        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        lay = QVBoxLayout(centralWidget)
        gv = MyGraphicsView()
        button = QPushButton("scale")
        lay.addWidget(gv)
        lay.addWidget(button)
        button.clicked.connect(gv.scale_pixmap)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())