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”连接起来的方法' 的功能,但没有一个起作用。我在寻找使用图形视图框架的方法的好示例时遇到了一些麻烦(大多数规模都非常小),所以如果我做得完全错误,请告诉我。
任何帮助表示赞赏。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难判断您的代码有什么问题,因为它不完整并且缺少一些部分来编译它。尽管有几个地方可能会导致问题:
下面是代码的简化版本,它将 ab 图像加载到标题中并将其添加到图形场景中:
希望这有帮助,问候
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:
Below is simplified version of your code which loads ab image into the Title and adds it to the graphic scene:
hope this helps, regards