0

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.

4

1 回答 1

1

很难判断您的代码有什么问题,因为它不完整并且缺少一些部分来编译它。虽然有几个地方可能会导致问题:

  1. 您的 Title 类构造函数;我相信你应该通过执行来调用基类构造函数:QtGui.QGraphicsPixmapItem.__init__(self, parent)
  2. 看起来您的图形场景对象正在按钮的 onclick 信号中构建。您的信号连接到正确的插槽可能存在问题,如果您的小部件中存在此类问题,您应该在输出中看到警告。
  3. 看起来您正在从 xml 文件中加载图像文件名,很难检查那里的逻辑是否正确,但您也可能在那里遇到问题。

下面是您的代码的简化版本,它将 ab 图像加载到标题中并将其添加到图形场景中:

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

希望这会有所帮助,问候

于 2010-09-11T00:11:10.280 回答