Results 1 to 8 of 8

Thread: Deleting and re-creating a QGraphicsScene

  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Deleting and re-creating a QGraphicsScene

    I've got a widget with a QGraphicsView and a QGraphicsScene, shown below.
    I want to delete all items from the scene and create new ones, so I'm trying to delete the scene and create a new one.

    myGraphicsWidget Constructor:
    Qt Code:
    1. myGraphicsWidget::myGraphicsWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. //Create a new scene
    5. myScene = new QGraphicsScene(this);
    6. myScene->setSceneRect(0, 0, 300.0, 300.0 );
    7.  
    8. //Creates items in scene myScene
    9. populateScene(myScene);
    10.  
    11. myView = new QGraphicsView(myScene);
    12. myView->setRenderHints(QPainter::Antialiasing);
    13.  
    14. QVBoxLayout *myLayout = new QVBoxLayout;
    15. myLayout->addWidget(myView);
    16. setLayout(myLayout);
    17. }
    To copy to clipboard, switch view to plain text mode 

    newScene:
    Qt Code:
    1. void myGraphicsWidget::newScene(void)
    2. {
    3. //Delete previous scene
    4. delete myScene;
    5.  
    6. //Create a new scene
    7. myScene = new QGraphicsScene(this);
    8.  
    9. //Creates items in scene myScene
    10. populateScene(myScene);
    11.  
    12. //Code here to update the widget/view???? <-- Need code here
    13. }
    To copy to clipboard, switch view to plain text mode 

    When I try to run newScene(), I get a blank Widget.
    Is there some code I need to insert at the end of newScene() to get it to update in the QGraphicsView and widget, or is this technique of deleting, creating and repopulating the scene not going to work?
    Should I be deleting the individual items from the scene instead of deleting the whole scene?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Deleting and re-creating a QGraphicsScene

    You also need to set the scene in the view.
    You do this in the constructor of your widget via myView = new QGraphicsView(myScene);

    I guess, I didn't check though, that there is a setScene(...) function in QGraphicsView

  3. #3
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting and re-creating a QGraphicsScene

    Yeah, I actually just realised before checking for a reply. It worked when I tried!
    Sorry for all these silly questions... I really need to look harder before asking, but sometimes I just never see the answers no matter how hard I look.

    It's not a 'bad programming practice' to delete and recreate a scene like that, is it?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Deleting and re-creating a QGraphicsScene

    Quote Originally Posted by dohzer View Post
    It's not a 'bad programming practice' to delete and recreate a scene like that, is it?
    It depends. What do you mean with changing items?
    If you only want to replace a couple of items in your scene, or change some properties, then the overhead of deleting the scene and repopulating it is very high.

    The QGraphicsView classes are based on the model/view design pattern. This means the data is separate from the actual painting. This is nice, since you don't have to do the paint updating yourself, you just change your data. Custom items do need to implement painting routines though (which is normal). But this also means you can extend the scene class for example and add more control features to it. Since the scene contains a list of all your items, you can use and extend the scene to do specific operations. For example: set the pen color of all lines to red.

    This of course means you need to implement some functions like setLineColor(color) which iterates the items in the scene, looks if an item is a line (for example) and then sets the color.

  5. #5
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Deleting and re-creating a QGraphicsScene

    Quote Originally Posted by dohzer View Post
    It's not a 'bad programming practice' to delete and recreate a scene like that, is it?
    Like tbscope said, it depends. However, just out of curiosity, did you happen to notice the clear() slot in QGraphicsScene? Alternately, you could roll your own using items(), which returns a list of all items on the scene for you to iterate through.

  6. #6
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting and re-creating a QGraphicsScene

    Quote Originally Posted by Urthas View Post
    Like tbscope said, it depends. However, just out of curiosity, did you happen to notice the clear() slot in QGraphicsScene? Alternately, you could roll your own using items(), which returns a list of all items on the scene for you to iterate through.
    I want to get rid of all items and create new ones, and the clear() function won't delete the items from memory, will it?
    I was thinking of using items() like you say, but there's not much point if I'm simply deleting all the items, is there?

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting and re-creating a QGraphicsScene

    I want to get rid of all items and create new ones, and the clear() function won't delete the items from memory, will it?
    From the docs -
    void QGraphicsScene::clear () [slot]

    Removes and deletes all items from the scene, but otherwise leaves the state of the scene unchanged.

  8. #8
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting and re-creating a QGraphicsScene

    I'm really going to have to learn to read the documentation more thoroughly.
    I could swear I was reading a thread (I believe it was from another Qt forum... no idea of the link) and they said that clear() didn't delete the items that were in the scene from program memory; it just removed them from the scene. Perhaps they were talking about a previous version of Qt, but more likely I just read it wrong.

Similar Threads

  1. deleting QStringList
    By timmu in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2009, 13:36
  2. creating/deleting Objects // memory leaks
    By janus in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 18:17
  3. Deleting QProcess
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 29th January 2008, 18:55
  4. deleting qaction
    By hyling in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2006, 21:48
  5. Creating, deleting folders with C++?
    By pir in forum General Programming
    Replies: 7
    Last Post: 1st August 2006, 11:18

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.