Results 1 to 5 of 5

Thread: Can't manually update QGraphicsScene

  1. #1
    Join Date
    Jan 2010
    Location
    Istanbul, Turkey
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't manually update QGraphicsScene

    I'm working with PyQt4.5.4.

    Summary: QGraphicsScene.update() method doesn't update the scene immediately.

    Detailed Description:
    I have a QGraphicsScene, with drawBackground method to draw background grids.

    Qt Code:
    1. class Scene(QGraphicsScene):
    2. ...............
    3.  
    4. def drawBackground(self, painter, rect):
    5. if self.gridActive:
    6. gridSize = 50
    7. left = int(rect.left()) - (int(rect.left()) % gridSize)
    8. top = int(rect.top()) - (int(rect.top()) % gridSize)
    9. lines = []
    10. right = int(rect.right())
    11. bottom = int(rect.bottom())
    12. for x in range(left, right, gridSize):
    13. lines.append(QLineF(x, rect.top(), x, rect.bottom()))
    14. for y in range(top, bottom, gridSize):
    15. lines.append(QLineF(rect.left(), y, rect.right(),y))
    16.  
    17. painter.setPen(QPen(Qt.lightGray))
    18. painter.drawLines(lines)
    To copy to clipboard, switch view to plain text mode 

    I want to be able to toggle the grid by using the menu:

    Qt Code:
    1. class MainWindow(QMainWindow):
    2. .......
    3.  
    4. def createActions(self):
    5. displayGrid = QAction('Display Grid', self)
    6. displayGrid.setCheckable(True)
    7. displayGrid.setChecked(self.scene.gridActive)
    8. displayGrid.setShortcut('Ctrl+G')
    9. displayGrid.setStatusTip('Display or Hide the grid')
    10.  
    11. self.connect(displayGrid, SIGNAL('toggled(bool)'), self.setDisplayGrid)
    12. viewMenu = self.menubar().addMenu('&View')
    13. viewMenu.addAction(displayGrid)
    14.  
    15. def setDisplayGrid(self, value):
    16. self.scene.gridActive = value
    17. self.scene.update()
    To copy to clipboard, switch view to plain text mode 

    (scene.gridActive is initially True)

    But the self.scene.update() method doesn't update it immediately. But rather I have to zoom in/out or resize the window to make the toggle effect get work.

    I used to be able to do it but somehow corrupted it and can't find the source of the problem. Any ideas?

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

    Default Re: Can't manually update QGraphicsScene

    We'd have to see the bigger picture. It is likely you are drawing outside boundingRects of your items or something like that.
    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
    Jan 2010
    Location
    Istanbul, Turkey
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't manually update QGraphicsScene

    Hmm,

    I tried self.view.fitInView(self.scene.itemsBoundingRect() ,Qt.KeepAspectRatio) but it also didn't work.

    I don't know if you'd mind looking at the big picture but here is the complete code:
    (unfortunately I'm using some other lib for data import. The Scene/View and MainWindow implementations are at the bottom. Top classes are not important)

    http://pastebin.com/m264f6728

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

    Default Re: Can't manually update QGraphicsScene

    It would be best to isolate the problem using a minimal compilable example.
    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.


  5. #5
    Join Date
    Jan 2010
    Location
    Istanbul, Turkey
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't manually update QGraphicsScene

    Oopps, I'm a bit dizzy late at night. I've produced a minimal example and figured out that the problem is with the line:

    Qt Code:
    1. self.setCacheMode(QGraphicsView.CacheBackground)
    To copy to clipboard, switch view to plain text mode 

    Removing it solved the problem. I just tought it could speed up the performance of drawing grid.

    Thank you for your help.

Similar Threads

  1. help for compile manually the Qt with Phonon
    By villa in forum Installation and Deployment
    Replies: 1
    Last Post: 3rd May 2009, 12:34
  2. QGraphicsScene update?
    By Gurdt in forum Qt Programming
    Replies: 11
    Last Post: 17th April 2009, 09:03
  3. QList resize manually
    By nina1983 in forum Qt Programming
    Replies: 4
    Last Post: 12th August 2008, 16:42
  4. Replies: 1
    Last Post: 26th December 2007, 10:58
  5. Manually using RCC compiler?
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2007, 07:47

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.