0
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 已经在那里了。在滚动条的屏幕上显示它时增加它的大小的方法是什么?

目的是有一个按钮,点击它的图片的大小应该会增加。

4

1 回答 1

1

你必须使用setScale(). 此外,当您使用addPixmap()它时,它会返回创建的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_())
于 2018-04-12T07:04:36.097 回答