PDA

View Full Version : QGraphicsView not displaying QGraphicsItems



person
10th September 2010, 05:13
Using PyQt4.

My goal is to load in "parts" of a .png, assign them to QGraphicsItems, add them to the scene, and have the QGraphicsView display them. (Right now I don't care about their coordinates, all I care about is getting the darn thing to work).

Currently nothing is displayed. At first I thought it was a problem with items being added and QGraphicsView not updating, but after reading up a bit more on viewports, that didn't really make sense. So I tested adding the QGraphicsView items before even setting the view (so I know it wouldn't be an update problem) and it still displayed nothing. The path is definitely correct. Here is some code that shows what is going on...



class TileBar(QtGui.QGraphicsScene):
def __init__(self, parent = None):
QtGui.QGraphicsScene.__init__(self, parent)

def loadTiles(self, filename):
tree = ElementTree()
tree.parse(filename)
root = tree.getroot()

sheets = root.findall('sheet')

for sheet in sheets:
sheetPath = sheet.get('path')
sheetImg = QtGui.QImage(sheetPath)

strips = sheet.findall('strip')
for strip in strips:
tile = Tile()
tile.idAttr = strip.get('id')

clip = strip.find('clip')
x = clip.get('x')
y = clip.get('y')
width = clip.get('width')
height = clip.get('height')

subImg = sheetImg.copy(int(x), int(y), int(width), int(height))
pixmap = QtGui.QPixmap.fromImage(subImg)
tile.setPixmap(pixmap)

self.addItem(tile)


*Note that the Tile class is just a QGraphicsItem (inherits it) with an idAttr attribute. That is it.

and...



class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)

self.setWindowTitle('NT State Editor')

winWidth = 1024
winHeight = 768

screen = QtGui.QDesktopWidget().availableGeometry()
screenCenterX = (screen.width() - winWidth) / 2
screenCenterY = (screen.height() - winHeight) / 2
self.setGeometry(screenCenterX, screenCenterY, winWidth, winHeight)

self.tileMap = tilemap.TileMap()
self.tileBar = tilebar.TileBar()

mapView = QtGui.QGraphicsView(self.tileMap)
tileBarView = QtGui.QGraphicsView(self.tileBar)

button = tilebar.LoadTilesButton()
QtCore.QObject.connect(button, QtCore.SIGNAL('selectedFile'),
self.tileBar.loadTiles)

hbox = QtGui.QHBoxLayout()
hbox.addWidget(mapView)
hbox.addWidget(self.tileBarView)
hbox.addWidget(button)

mainWidget = QtGui.QWidget()
mainWidget.setLayout(hbox)

self.setCentralWidget(mainWidget)


app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())


I tried some stuff with connecting the TileBar's 'changed()' signal with various 'view' functions, but none of them worked. I've had a bit of trouble finding good examples of ways to use the Graphics View Framework, (most are very very small scale) so let me know if I'm doing it completely wrong.

Any help is appreciated. Thanks.

wysota
10th September 2010, 09:29
Could you show us your implementation of the Tile class?

person
10th September 2010, 18:27
class Tile(QtGui.QGraphicsPixmapItem):
def __init__(self, parent = None):
QtGui.QGraphicsScene.__init__(self, parent)

self.idAttr = -1


Wow, and after typing that I realize what is wrong. Can't test right now, but will let you know if that is the problem.

person
10th September 2010, 19:12
Gah, still nothing displayed. Obviously I fixed the previous code with QGraphicsPixmapItem.__init__(self, parent).

wysota
10th September 2010, 19:18
What if you substitute Tile with QGraphicsPixmapItem?

person
11th September 2010, 01:00
Still doesn't work.

person
11th September 2010, 02:08
I've isolated the problem to the copy function when loading the tiles. For some reason, it makes all the size members 0 in the sub image.

person
11th September 2010, 02:20
Solved, path loaded in from xml file was relative, so no png was even loaded in.