2010-09-10 57 views
0

使用PyQt4。QGraphicsView不顯示QGraphicsItems

我的目標是加載一個.png的「部分」,將它們分配給QGraphicsItems,將它們添加到場景中,並讓QGraphicsView顯示它們。 (現在我不在乎他們的座標,我所關心的只是讓這件事情起作用)。

當前沒有顯示。起初我認爲這是一個項目被添加和QGraphicsView沒有更新的問題,但在閱讀了一些視口後,這並沒有什麼意義。所以我測試添加QGraphicsView項目甚至設置視圖之前(所以我知道它不會是一個更新問題),它仍然沒有顯示任何東西。路徑絕對正確。下面是一些代碼,顯示是怎麼回事...

忽略間距問題,粘貼

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) 

我嘗試了一些東西帶將TileBar的'changed()'信號與各種'視圖'功能連接起來,但沒有一個能工作。我在找到使用Graphics View Framework的方法的好例子時遇到了一些問題(大多數規模都非常小),所以讓我知道我是否完全錯誤。

任何幫助表示讚賞。謝謝。

回答

1

很難說出你的代碼有什麼問題,因爲它不完整,並且缺少一些可以編譯的部分。雖然有幾個地方可能會導致問題:

  1. Your Title class constructor;我相信你應該通過執行: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_() 

希望這會有所幫助,至於

+0

你目不轉睛地權上的構造,我也注意到了。但是,問題實際上是獲取子圖像,出於某種原因轉換爲int不起作用。立即修復。 – random 2010-09-11 00:59:36

+0

是的,問題出在xml文件中,因爲路徑是相對的。 – random 2010-09-11 01:19:48

相關問題