QGraphicsView i QGraphicsScene
15 December 2008
Comments
QGraphicsView to klasa dostarczająca widżet wizualizujący obiekty QGraphicsScene - powierzchni zawierających dużą ilość graficznych elementów 2D. Scena (QGraphicsScene) może zawierać różne elementy (dodawane za pomocą addEllipse(), addLine(), addPath(), addPixmap(), addPolygon(), addRect() lub addText()) i wyświetlać je w zadany sposób. Oto przykład wyświetlenia pliku graficznego:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication(sys.argv)
grview = QGraphicsView()
scene = QGraphicsScene()
scene.addPixmap(QPixmap('pic.jpg'))
grview.setScene(scene)
grview.show()
sys.exit(app.exec_())
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtOpenGL import *
app = QApplication(sys.argv)
grview = QGraphicsView()
grview.setViewport(QGLWidget())
scene = QGraphicsScene()
scene.addPixmap(QPixmap('pic.jpg'))
grview.setScene(scene)
grview.show()
sys.exit(app.exec_())
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class ImageView(QGraphicsView):
def __init__(self, parent=None, origPixmap=None):
"""
QGraphicsView that will show an image scaled to the current widget size
using events
"""
super(ImageView, self).__init__(parent)
self.origPixmap = origPixmap
QMetaObject.connectSlotsByName(self)
def resizeEvent(self, event):
"""
Handle the resize event.
"""
size = event.size()
item = self.items()[0]
# using current pixmap after n-resizes would get really blurry image
#pixmap = item.pixmap()
pixmap = self.origPixmap
pixmap = pixmap.scaled(size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.centerOn(1.0, 1.0)
item.setPixmap(pixmap)
app = QApplication(sys.argv)
pic = QPixmap('pic.jpg')
grview = ImageView(origPixmap=pic)
scene = QGraphicsScene()
scene.addPixmap(pic)
grview.setScene(scene)
grview.show()
sys.exit(app.exec_())
RkBlog
Comment article