PDA

View Full Version : how to modify contents of QScrollArea?



cyrfer
17th January 2010, 23:56
Hi,
I'd like to make a 'quiz' and I assumed I should add the long list of questions and radiobutton answers into a QScrollArea so they could all be seeen by the user. I'm having trouble to populate the scrollarea procedurally. In the Designer, I've been able to add mock-up questions to the scrollarea, but I can't even remove these mock-ups using my C++ code. I assume that if I can delete these children, I will know how to add content too.

I tried iterating over the "children of the scrollbar's widget's layout" (it hurts a little to say that). Here is my code to do that, which has a bad side-effect; the children render in a different place instead of just being deleted. I'd like to know the right way to delete the content of a scrollarea, thanks for any tips!



QWidget* scrollarea_content = ui->_quiz_scrollarea->widget();
if( scrollarea_content )
{
//what is this? scrollarea_content->children();

QLayout* scrollarea_layout = scrollarea_content->layout();

if( scrollarea_layout )
{
QLayoutItem *child;
while ((child = scrollarea_layout->takeAt(0)) != 0)
{
delete child;
}
}
}

mattc
18th January 2010, 10:28
I believe the problem is that you are not resetting the widget parent (i.e. you are removing it from the layout, but it is still a child of the scroll area)

Try something like this:


QLayoutItem *child;

while ((child = scrollarea_layout->takeAt(0)) != 0)
{
child->widget()->setParent(0);
delete child;
}
(I am sorry I cannot test the code at the moment)