views:

87

answers:

1

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...

Ignore spacing issues, layout got messed up when pasting

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_())

--

class Tile(QtGui.QGraphicsPixmapItem):
    def __init__(self, parent = None):
        QtGui.QGraphicsPixmapItem(self, parent)
        self.idAttr = -1


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)

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.

A: 

It's quite hard to tell what's wrong with your code as it's not complete and missing some parts to get it compiled. Though there are couple of places which could potentially cause the problem:

  1. Your Title class constructor; I believe you should be calling the base class constructor there by executing: QtGui.QGraphicsPixmapItem._init_(self, parent).
  2. Looks like your graphic scene objects are getting constructed in the button's onclick signal. There could be problems with your signal connecting to the proper slot, you should see warnings in the output if there are such problems in your widget.
  3. It looks like you're loading images file names from the xml file, it's quite hard to check if the logic over there is straight but potentially you could have a problem over there too.

Below is simplified version of your code which loads ab image into the Title and adds it to the graphic scene:

import sys
from PyQt4 import QtGui, QtCore

class Tile(QtGui.QGraphicsPixmapItem):
    def __init__(self, parent=None):
        QtGui.QGraphicsPixmapItem.__init__(self, parent)
        self.idAttr = -1

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

    #def loadTiles(self, filename):
    def loadTiles(self):

        sheetImg = QtGui.QImage("put_your_file_name_here.png")
        pixmap = QtGui.QPixmap.fromImage(sheetImg)

        tile = Tile()
        tile.setPixmap(pixmap)

        self.addItem(tile)

        # skipping your ElementTree parsing logic here

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 = Tiletilebar.Map()
        self.tileBar = TileBar()

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

        #button = self.tilebar.LoadTilesButton()
        button = QtGui.QPushButton() 
        QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), 
                               self.tileBar.loadTiles)

        #self.tileBar.loadTiles('some_file_name')

        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([])
exm = MainWindow()
exm.show()
app.exec_()

hope this helps, regards

serge_gubenko
You wre right on the constructor, I noticed that as well. However, the problem was actually in getting the sub image, for some reason the conversion to int isn't working. Fixing right now.
random
Yes, the problem was in the xml file because the path was relative.
random