PDA

View Full Version : How do I replace a widget?



pir
23rd July 2006, 21:41
Hi!

I have a GLWidget that shows a 3d scene. When I open a file with a scene in, I need to replace that GLwidget with a new GLwidget with the loaded scene. The problem is that the program spits out a lot of exceptions when I close the program. So I suppose that the widget holding the GLWidget still tries to destroy the old GLWidget when I close the program. I'll spare you the long list of messages, but it starts with ***glibc detected*** double free or corruption.
Is there any way to solve this?

This is my attempt to do this:


/*
canvas is the GLWidget holding the 3d scene.
*/
mainLayout->removeWidget ( canvas );
canvas->hide ( );
canvas->setParent ( NULL )
delete canvas;

/*
Creates the new GLCanvas containing the new loaded scene....

*/
canvas = new GLCanvas ( newScene, this );
mainLayout->addWidget ( canvas );


hope I've explained the problem in a understandable way...
thanks for reading
pir

wysota
24th July 2006, 02:01
Why do you have to replace the widget? Maybe you could make the widget able to handle both scenes and just replace the scene and not the widget?

pir
24th July 2006, 13:52
A good point.
But what if I later on want to be able to have access to the old scene from another widget. In that case, it makes it a bit more complex to insert the new information in the already existing GLWidget. Because then I would use the old memory address and pointers pointing to this adress in belief that it contains the old information would be suprised.

But I've found a solution to the problem.

I've forgot something very important in my question. Was to tired and anoyed:

the GLWidget class called GLCanvas has two parents, GLWidget and Scene. Where class Scene is a class with all the 3d rendering stuff. So the solution was simply to create a new GLCanvas and insert the loaded scene information in it by calling Scene::operator=(const Scene&)... I tried to do this at contruction of the GLCanvas, but there seems to be something I don't know about Qt and copy constructors and operator=. Because it didn't work if I passed the loaded scene into the constructor...

thanks
pir

wysota
24th July 2006, 16:11
A good point.
But what if I later on want to be able to have access to the old scene from another widget.
But you are deleting the widget so how do you want to access it later?


In that case, it makes it a bit more complex to insert the new information in the already existing GLWidget. Because then I would use the old memory address and pointers pointing to this adress in belief that it contains the old information would be suprised.
I don't really... understand...

Maybe all you need is QStackedWidget?

pir
24th July 2006, 16:51
Yes, at the current implementation I am deleting the old scene. But I think there may come up situations where I need the old scene in future versions of the program. If that is the case, I only need to erase the delete command. But I'm not sure about what kind of situation that would be, but I thought that if it didn't cost too much effort do this, it could be a nice way to handle it.

Maybe if I have several widgets displaying the same scene but from different cameras and I for some reason want to open a new scene in one of the widgets and still having the old scene in the others... sure there are a lot of ways to solve this. But I think that, by doing this, I maybe have more flexibility. And since I managed to do this, it's was easier to implement than changing to a stacked widget or something similar.


thanks
pir