[英]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()
这将返回创建的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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.