Results 1 to 8 of 8

Thread: QGraphicsView not displaying QGraphicsItems

  1. #1
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsView not displaying QGraphicsItems

    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...

    Qt Code:
    1. class TileBar(QtGui.QGraphicsScene):
    2. def __init__(self, parent = None):
    3. QtGui.QGraphicsScene.__init__(self, parent)
    4.  
    5. def loadTiles(self, filename):
    6. tree = ElementTree()
    7. tree.parse(filename)
    8. root = tree.getroot()
    9.  
    10. sheets = root.findall('sheet')
    11.  
    12. for sheet in sheets:
    13. sheetPath = sheet.get('path')
    14. sheetImg = QtGui.QImage(sheetPath)
    15.  
    16. strips = sheet.findall('strip')
    17. for strip in strips:
    18. tile = Tile()
    19. tile.idAttr = strip.get('id')
    20.  
    21. clip = strip.find('clip')
    22. x = clip.get('x')
    23. y = clip.get('y')
    24. width = clip.get('width')
    25. height = clip.get('height')
    26.  
    27. subImg = sheetImg.copy(int(x), int(y), int(width), int(height))
    28. pixmap = QtGui.QPixmap.fromImage(subImg)
    29. tile.setPixmap(pixmap)
    30.  
    31. self.addItem(tile)
    To copy to clipboard, switch view to plain text mode 

    *Note that the Tile class is just a QGraphicsItem (inherits it) with an idAttr attribute. That is it.

    and...

    Qt Code:
    1. class MainWindow(QtGui.QMainWindow):
    2. def __init__(self, parent = None):
    3. QtGui.QMainWindow.__init__(self, parent)
    4.  
    5. self.setWindowTitle('NT State Editor')
    6.  
    7. winWidth = 1024
    8. winHeight = 768
    9.  
    10. screen = QtGui.QDesktopWidget().availableGeometry()
    11. screenCenterX = (screen.width() - winWidth) / 2
    12. screenCenterY = (screen.height() - winHeight) / 2
    13. self.setGeometry(screenCenterX, screenCenterY, winWidth, winHeight)
    14.  
    15. self.tileMap = tilemap.TileMap()
    16. self.tileBar = tilebar.TileBar()
    17.  
    18. mapView = QtGui.QGraphicsView(self.tileMap)
    19. tileBarView = QtGui.QGraphicsView(self.tileBar)
    20.  
    21. button = tilebar.LoadTilesButton()
    22. QtCore.QObject.connect(button, QtCore.SIGNAL('selectedFile'),
    23. self.tileBar.loadTiles)
    24.  
    25. hbox = QtGui.QHBoxLayout()
    26. hbox.addWidget(mapView)
    27. hbox.addWidget(self.tileBarView)
    28. hbox.addWidget(button)
    29.  
    30. mainWidget = QtGui.QWidget()
    31. mainWidget.setLayout(hbox)
    32.  
    33. self.setCentralWidget(mainWidget)
    34.  
    35.  
    36. app = QtGui.QApplication(sys.argv)
    37. mainWindow = MainWindow()
    38. mainWindow.show()
    39. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not displaying QGraphicsItems

    Could you show us your implementation of the Tile class?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView not displaying QGraphicsItems

    Qt Code:
    1. class Tile(QtGui.QGraphicsPixmapItem):
    2. def __init__(self, parent = None):
    3. QtGui.QGraphicsScene.__init__(self, parent)
    4.  
    5. self.idAttr = -1
    To copy to clipboard, switch view to plain text mode 

    Wow, and after typing that I realize what is wrong. Can't test right now, but will let you know if that is the problem.

  4. #4
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView not displaying QGraphicsItems

    Gah, still nothing displayed. Obviously I fixed the previous code with QGraphicsPixmapItem.__init__(self, parent).

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not displaying QGraphicsItems

    What if you substitute Tile with QGraphicsPixmapItem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView not displaying QGraphicsItems

    Still doesn't work.

  7. #7
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView not displaying QGraphicsItems

    I've isolated the problem to the copy function when loading the tiles. For some reason, it makes all the size members 0 in the sub image.

  8. #8
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView not displaying QGraphicsItems

    Solved, path loaded in from xml file was relative, so no png was even loaded in.

Similar Threads

  1. Replies: 16
    Last Post: 29th March 2010, 07:05
  2. Moving QGraphicsItems and Fitting QGraphicsView
    By aladagemre in forum Qt Programming
    Replies: 0
    Last Post: 30th January 2010, 18:21
  3. Unselect QGraphicsItems
    By jano_alex_es in forum Newbie
    Replies: 1
    Last Post: 10th September 2009, 17:19
  4. PopupMenu for several QGraphicsItems
    By jobrandt in forum Qt Programming
    Replies: 0
    Last Post: 10th August 2009, 15:00
  5. QGraphicsItems on top of each other?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2006, 20:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.