QPixmap:如何在addPixmap中增加图片的大小?

时间:2018-04-12 06:51:02

标签: python pyqt pyqt5 qgraphicsscene qgraphicspixmapitem

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

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

1 个答案:

答案 0 :(得分:1)

您必须使用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_()) ,但在这种情况下,更好的选择是将其置于图像的中心。

session