QGraphicsView不显示QGraphicsItems
我在用PyQt4。
我的目标是加载一部分.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()'信号和各种'view'函数连接起来,但都没有成功。我在找使用Graphics View Framework的好例子时遇到了一些麻烦(大多数例子都非常小),所以如果我完全搞错了,请告诉我。
任何帮助都很感激。谢谢。
1 个回答
2
很难判断你的代码哪里出错,因为它不完整,缺少一些编译所需的部分。不过,有几个地方可能会导致问题:
- 你的 Title 类的构造函数;我觉得你应该在这里调用父类的构造函数,也就是执行:QtGui.QGraphicsPixmapItem.__init__(self, parent)。
- 看起来你的图形场景对象是在按钮点击时创建的。如果信号没有正确连接到相应的槽,可能会出现问题。如果你的控件有这样的错误,输出中应该会有警告信息。
- 你似乎是从 xml 文件中加载图片的文件名,检查那里的逻辑是否正确可能有点困难,但那里也可能会出现问题。
下面是你代码的简化版本,它将一张图片加载到 Title 中,并将其添加到图形场景中:
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_()
希望这能帮到你,祝好