How to remove all items from a GraphicsView

5,010 views
Skip to first unread message

Julio Trevisan

unread,
Mar 25, 2013, 2:11:19 PM3/25/13
to pyqt...@googlegroups.com
Hi,

How can I remove all elements from a GraphicsView? I am trying to do something like this:

        gv = self.graphicsView
        for item in gv.items():
            gv.removeItem(item)


... giving me this kind of error

Exception RuntimeError: RuntimeError('maximum recursion depth exceeded\n(offending frame is two down the stack)',) in <bound method Proxy.__del__ of <Pyro4.core.Proxy at 0x44b0550, not connected, for PYRONAME:navala.HTTPClient>> ignored

QGraphicsScene::removeItem: item 0x3afe410's scene (0x0) is different from this scene (0x3ad37c0)

QGraphicsScene::removeItem: item 0x45578f0's scene (0x0) is different from this scene (0x3ad37c0)

QGraphicsScene::removeItem: item 0x4559ec0's scene (0x0) is different from this scene (0x3ad37c0)


I also tried gv.scene().clear() , but then it gives an error in GraphicsView.py, here:

    def setCentralWidget(self, item):
        """Sets a QGraphicsWidget to automatically fill the entire view (the item will be automatically
        resize whenever the GraphicsView is resized)."""
        if self.centralWidget is not None:
            self.scene().removeItem(self.centralWidget)
  <--

"unhandled RuntimeError, wrapped C/C++ object of %s has been deleted"

Setting gv.centralWidget = None doesn't solve it either, as I get the same error message as above elsewhere.

Any ideas, please?

Luke Campagnola

unread,
Mar 25, 2013, 2:35:30 PM3/25/13
to pyqt...@googlegroups.com
Hi Julio,
I think for me to really answer this question, you should tell me exactly why you're clearing the GraphicsView or how it will be used (if at all) after it is cleared. If you want to swap out the central graphics item for another, you can call:

    gv.setCentralItem(newItem)

This should clear out everything else in the view that was a child of the previous central item.
If you want to clear the view, with a chance of setting a new central item later, then:

  gv.scene.clear()
  gv.centralWidget = None

I am modifying the API such that, in future releases, calling gv.setCentralItem(None) will work correctly.
Does this answer your question?


Luke

Julio Trevisan

unread,
Apr 6, 2013, 7:05:24 PM4/6/13
to pyqt...@googlegroups.com
Hi Luke

Sorry for late reply. It took me a whie to figure out what was going on. Using gv.setCentralItem(newItem), as you suggested, did seem to work. However, I implemented a crosshair based on one of your examples and I noticed that the old items (i.e. the ones under the old central item) keep sending mouseMove events. I guess the reference count of the old items will not be zero either, so they are kept in memory with all their data. I am quite a beginner in Qt, it is really hard for me to debug this.

Thanks for your help

Julio Trevisan

unread,
Apr 7, 2013, 3:16:03 PM4/7/13
to pyqt...@googlegroups.com
I kind of solved the issue of deleting all and re-adding new items to a GraphicsView. The solution that I found was to re-create the GraphicsView. Please see code below for reference.

import pyqtgraph as gr

.
.
.


    def MakeGrid(self):

        .
        .  (bit of code that mounts the central item, p)
        .

        # Removes previous items in layout
        # Reference: http://stackoverflow.com/questions/4528347/clear-all-widgets-in-a-layout-in-pyqt
       
for i in reversed(range(self.framePlot.layout().count())):
            self.framePlot.layout().itemAt(i).widget().setParent(None)
        # Creates and adds GraphicsView to layout
       
gv = gr.GraphicsView()
        self.framePlot.layout().addWidget(gv)
        gv.setCentralItem(p) # p is a PlotGrid, descends from gr.GraphicsLayout 



--
-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]
---
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages