PDA

View Full Version : QGraphicsView



Yayati.Ekbote
4th March 2010, 12:52
hi,

I haven't been able to use QGraphicsView. I didn't understand the concept of QScene. Even i can't use QImage to display an image through QGraphicsView. What shoul i do? Plz guide!!

yogeshgokul
4th March 2010, 13:06
hi,

I haven't been able to use QGraphicsView. I didn't understand the concept of QScene. Even i can't use QImage to display an image through QGraphicsView. What shoul i do? Plz guide!!

You can have a look, in Qt Docs regarding the basic of model view architecture. For now, Qt's MVA is rich set of classes helps you to create rich graphical applications. And this is efficient also, to give you good performance. There are 3 main classes in this MVA.
1. The QGraphicsItem represents the item to be drawn/shown. It could be anything like a line, rect, ellipse, curve, image. So we need to use appropriate class for the customized usage.
2. The QGraphiceScene is a canvas like background where all kind of graphics item resides. The scene has full control over all items. All kind of drawing happens on the scene.
3. The QGraphicsView is a Qt widget, which acts like the parent for QGraphicsScene. This just provides the place where you can use scene. And if you can use scene, you can use items.

prof.ebral
4th March 2010, 16:10
The VGT I am working on is GPL and Open Source. While making the switch I have been able to rewrite portions of the old and dusty code so that portions of it could be lent to other softwares. Here is a link so you can download the full source: http://my.pogoplug.com/share/RPBBQftK2AaDIG99LwumcA/

This is what I am doing. First .. I have ripped apart the UI and the calculations so the code you are going to see is just a widget that will be docked into the UI. Also, the code is in PyQt, but with some translation you should be able to see how it could fit in pure Qt.



# Maintainer (Traipse): Tyler Starke
# Version: Traipse 'Pious-Paladin'
# $Id: main.py,v Traipse 'Pious-Paladin' prof.ebral Exp $
#
# Description: Base Map Gear

"""
Planned Implementation:

The map will change first and foremost by creating a new layer called the Table Layer. This layer will be just that, a table.

The Background layer will be a bit different
"""

from PyQt4 import QtGui, QtCore
from tool_gears.path_gear import path
from map_engine.scroll_gear import ScrollGear
from map_engine.mini_gear import MiniGear

class MapDocket(QtGui.QGraphicsView):

def __init__(self):
super(MapDocket, self).__init__()
self.createDocket()
self.createMap()
self.setScene(self.mapView)
self.centerOn(0,0)
self.show()

def createDocket(self):
self.setBackgroundBrush(QtGui.QBrush(QtGui.QPixmap (path['textures']+'versa_anigre.jpg')))
self.setCacheMode(QtGui.QGraphicsView.CacheBackgro und)
self.setViewportUpdateMode(QtGui.QGraphicsView.Bou ndingRectViewportUpdate)
self.setDragMode(QtGui.QGraphicsView.ScrollHandDra g)

def createMap(self):
self.mapView = QtGui.QGraphicsScene()
#self.dropEvent = self.sceneDropEvent
self.mapView.setSceneRect(0, 0, 1000, 1000)
self.mapView.setItemIndexMethod(QtGui.QGraphicsSce ne.NoIndex)

item = MiniGear(path['textures']+'grass-natural.jpg')
self.mapView.addItem(item)
item = MiniGear(path['textures']+'water20.jpg')
self.mapView.addItem(item)

def dropEvent(self, evt):
print 'Dropped'


def wheelEvent(self, evt):
scale = evt.delta()
if scale < 0: x = .9; y = .9
if scale > 0: x = 1.1; y = 1.1
self.scale(x, y)


What I do is class a GraphicsView and then I create the scene in side and set the scene to the view. Then I can add items to the seen. See how I am adding items? Here is the class in its current state:



# Version: Traipse 'Pious-Paladin'
# $Id: main.py,v Traipse 'Pious-Paladin' prof.ebral Exp $
#
# Description: Base Map Gear

from PyQt4 import QtGui
from tool_gears.path_gear import path

class MiniGear(QtGui.QGraphicsPixmapItem):

def __init__(self, pixmap):
super(MiniGear, self).__init__()
self.setPixmap(QtGui.QPixmap(pixmap))
self.setAcceptDrops(True)
self.setAcceptHoverEvents(True)
self.setFlags(QtGui.QGraphicsItem.ItemIsMovable)

def dropEvent(self, evt):
print 'Dropped'

def dragMoveEvent(self, evt):
print 'Move'


I am creating items this way because it will give me a lot more options to manipulate the item later. If I create a dictionary of Map Minis, and I will, I can pull a stunt like this.



image = MiniGear(path['textures']+'grass-natural.jpg')
self.mapMinis[self.minis] = image
self.minis += 1


This puts that image into the dictionary, but the image will also have callable functions, practically inside itself. So later I will be able to do something like



self.mapMinis[0].addEffect( Effect )


And that item will have an effect added to it.