如何使图片缩放(QGraphicsItem),并使滚动条相应地改变它们的大小(QGraphicsScene)?

时间:2018-04-17 04:07:52

标签: python qt pyqt

如何缩放QGraphicsItem的绘制像素图?在那里找不到任何setScaled(),size或height属性。在图片变大之后,MyGraphicsView类的滚动条应该能够滚动大尺寸图片。

#!/usr/bin/env python

from PyQt5.QtCore import (QRectF, Qt)
from PyQt5.QtGui import (QPainter, QPixmap)
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem)


class TicTacToe(QGraphicsItem):
    def __init__(self, helper):
        super(TicTacToe, self).__init__()
        self.mypixmap = QPixmap("exit.png")

    def paint(self, painter, option, widget):
        painter.setOpacity(1)
        painter.drawPixmap(0,0, 300, 300, self.mypixmap)


    def boundingRect(self):
        return QRectF(0,0,300,300)


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

        self.tic_tac_toe = TicTacToe(self)

        scene.addItem(self.tic_tac_toe)

        self.setScene(scene)

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)


class Example(QMainWindow):    
    def __init__(self):
        super(Example, self).__init__()

        self.y = MyGraphicsView()
        self.setCentralWidget(self.y)

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

0 个答案:

没有答案