【发布时间】:2018-04-16 10:02:10
【问题描述】:
下例中如何将QGraphicsItem中绘制的像素图添加到QGraphicsScene中?
#!/usr/bin/env python
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QGraphicsItem, QGraphicsView, QGraphicsScene, QMainWindow
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_())
【问题讨论】:
-
抱歉,我不清楚这个问题。你的代码有什么问题?您要达到的目标是什么?你能告诉我们更多细节吗?
-
我需要用画家在像素图上绘制,缩放图片,并为其添加滚动条。这就是目的。 @S.Monteleone
-
@Aquarius_Girl。你为什么不使用QGraphicsPixmapItem?
-
@ekhumoro 如果我使用 qgraphicspixmapitem 那么我可以在场景中包含绘制的像素图吗?
-
@Aquarius_Girl。你不需要画任何东西——它会为你做所有的事情。