I have a QScrollArea with an included widget to select something. After the selection, the widget in the scroll area should switch to another widget. The user can also return and get the selection widget again to select something else.
But I always run into errors. It seems this is the problem:
QScrollArea -> SetWidget() : [...] The widget becomes a child of the scroll area and will be destroyed when the scroll area is deleted or when a new widget is set. [...]
This works fine. I add the selection widget like this in the constructor:
// Configure Window List setup
QVBoxLayout* sc_layout = new QVBoxLayout(&w_window_select);
// ... add some stuff into it ...
ui.scrollArea->setWidget(&w_window_select);
And I change the widget like this:
ui.scrollArea->setWidget(&w_window_select);
w_window_select.show();
// or
ui.scrollArea->setWidget(&lb_img);
lb_img.show();
In the beginning, I had a Pointer to w_window_select and lb_img saved in my class but as soon as I switched the widget the first time my old widget would get deleted, I think. I thought I can change it to normal class members to prevent the delete and it works, but it crashes too.
HEAP[Program.exe]: Invalid address specified to RtlValidateHeap( 0000000000350000, 000000000020F3C8 )
How can I solve this problem? I could create the widgets every time I need to switch again(they are not that big), but for me, this looks like a dumb solution.
The Answer was the comment from @G.M.
Not sure I completely understand your issue but I think QScrollArea::takeWidget might prove useful. It "removes the scroll area's widget, and passes ownership of the widget to the caller".
The code:
w_window_select = ui.scrollArea->takeWidget();
ui.scrollArea->setWidget(lb_img);
lb_img->show();
Take the widget before you switch to the new one.