Ad different widget in the same grid position

Dear Forum
In my GUI, I have a grid 1 row, 2 col ,so used:
-In col 0, some button
-In col 1, a object PlotWidget()

The PlotWidget() work very good, so , no preblem in set all the option
was mentioned in the book . Also updating it with random data in a timer was realy easy and nice to see.

The plot was placed in col 1,in this way:
self.Grid.addWidget(self.Obj_Plot_A,0,1)

Problem start when i want to visualize another plot in the same position:
self.Grid.addWidget(self.Obj_Plot_B,0,1)

where Obj_Plot_B have difefrent label, color ecc…
What happend is this:
1)I start with Obj_Plot_A ,and, all ok
2)When press a button, i replace with Obj_Plot_B, and all ok
3)replacing again with Obj_Plot_A is not working; the Obj_Plot_B
will remain visible, and A not.
I noted that, Obj_Plot_A is alwais there when replacing with _B,but, seems it moves in other position…

I also try with the removeWidget(), but not work:
self…Grid.removeWidget(self…Obj_Plot_B)
self.Grid.addWidget(self.Obj_Plot_A,0,1)

But worst again, if i try this:
self.Grid.removeWidget(self.Obj_Plot_A)
self.Grid.removeWidget(self.Obj_Plot_B)
they stay…??

—I also try this solution:
Try to avoid to mess with grid, try this solution :

     self.widplot=QMainWindow()
    self.widplot.setCentralWidget(self.Obj_Plot_At)

    self.Grid_1x2.addWidget(self.widplot,0,1)

In this way, i add the widplot to the grid, and dont change any more.
What i chenge will be, depending of what plot i want to visualize:

self.widplot.setCentralWidget(self.Obj_Plot_A)
or
self.widplot.setCentralWidget(self.Obj_Plot_B)

In this case ,I can see the see the Obj_Plot_A OK, then, change to _B
but imidiatly afer, a lot of this error:

self.updateItems( styleUpdate = self.property(‘styleWasChanged’) )
RuntimeError: Internal C++ object (PlotDataItem) already deleted.

Excuse for long message, and thanks for help
Roberto

Hello Roberto,

Did you also hide the widget after removing it from the layout ? as the doc stands: “After this call, it is the caller’s responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary”. As an example, I would try something like this:

self.Grid.removeWidget(self.Obj_Plot_A)
self.Obj_Plot_A.hide()
self.Grid.addWidget(self.Obj_Plot_B, 0, 1)
self.Obj_Plot_B.show()

And the opposite for showing Plot A and hiding Plot B

Alernatively, it may be simpler to use a QStackedWidget in your case, so you can switch easily from plot A to plot B, without having to remove/add them manually from layout.

Something like:

self.Stack = QStackedWidget()
self.Stack.addWidget(self.Obj_Plot_A)
self.Stack.addWidget(self.Obj_Plot_B)
# ...
self.Grid.addWidget(self.Stack, 0, 1)

Then when you want to switch between plots, simply set the index of the stack:

self.Stack.setCurrentIndex(1) # 0 for Plot A, 1 for Plot B