QGraphicsView 不显示 QGraphicsItems

发布于 2024-09-18 04:14:00 字数 2631 浏览 9 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2024-09-25 04:14:00

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

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

希望这有帮助,问候

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文