QGraphicsView not displaying QGraphicsItems
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...
Code:
def __init__(self, parent = None):
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...
Code:
def __init__(self, parent = None):
self.setWindowTitle('NT State Editor')
winWidth = 1024
winHeight = 768
screenCenterX = (screen.width() - winWidth) / 2
screenCenterY = (screen.height() - winHeight) / 2
self.setGeometry(screenCenterX, screenCenterY, winWidth, winHeight)
self.tileMap = tilemap.TileMap()
self.tileBar = tilebar.TileBar()
button = tilebar.LoadTilesButton()
QtCore.
QObject.
connect(button, QtCore.
SIGNAL('selectedFile'),
self.tileBar.loadTiles)
hbox.addWidget(mapView)
hbox.addWidget(self.tileBarView)
hbox.addWidget(button)
mainWidget.setLayout(hbox)
self.setCentralWidget(mainWidget)
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.
Re: QGraphicsView not displaying QGraphicsItems
Could you show us your implementation of the Tile class?
Re: QGraphicsView not displaying QGraphicsItems
Code:
def __init__(self, parent = None):
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.
Re: QGraphicsView not displaying QGraphicsItems
Gah, still nothing displayed. Obviously I fixed the previous code with QGraphicsPixmapItem.__init__(self, parent).
Re: QGraphicsView not displaying QGraphicsItems
What if you substitute Tile with QGraphicsPixmapItem?
Re: QGraphicsView not displaying QGraphicsItems
Re: QGraphicsView not displaying QGraphicsItems
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.
Re: QGraphicsView not displaying QGraphicsItems
Solved, path loaded in from xml file was relative, so no png was even loaded in.