Deleting data from a QScrollArea
-
Hmm.
Ok. I was kinda planning
to just create new widget with a boxlayout and call
setWidget on ScrollArea.
But u say this crashes? -
Hi again
i made same layout in designer
I kill them this wayvoid MainWindow::on_btremove_released() { QList<QWidget*> allSubLayouts = ui->innerwidget->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly); int size = allSubLayouts.size(); // take all out while (( ui->innerwidget->layout()->takeAt(0)) != 0) // killem for(int i = 0; i < size; i++) { qDebug() << allSubLayouts[i]->metaObject()->className(); delete allSubLayouts[i]; } }
U can check your self.
https://www.dropbox.com/s/auivviiv8tv7jfw/myscroll.zip?dl=0
should be the same you want if i read ur fine drawing correct :)
Note the Qt::FindDirectChildrenOnly flag! -
@mrjj
I don't want to delete my QWidget because I would have to dynamically create a new one each time I loaded a new file and I really don't want to deal with that.I'm making some progress with this:
while((child = mainVbox->layout()->takeAt(0)) != 0) { delete child; }
but in the documentation it says that when I delete a layout, I have to delete the widgets separately.
So I'm trying to make a list of the widgets found within the sublayout and then delete from there but it's not working. -
Did u try the sample?
It leaves the QWidget and layout but kills all it members. -
Hi
In the documentation for findChildren. I went looking as your
drawing made it very clear what u wanted. (kill all inner widgets)So If you do it for your Widget (myWidget), it should do the same.
( i hope :)you are welcome. I hope u get it to work.
-
@mrjj
So here is what I've ended up doing.I had been able to get a QList of the layouts in the main layout like so
QList<QLayout *> allSubLayouts = mainVbox->findChildren<QLayout *>();
So I checked if that list was empty or not, basically seeing if it was the first time to use the program.
if(!allSubLayouts.empty()) { int size = allSubLayouts.size(); for(int i = 0; i < size; i ++) { qDebug() << "this is what is at allSublayouts.at("<<i<<") " << allSubLayouts.at(i); for(int j = 0; j < allSubLayouts.at(i)->count(); ++j) { QWidget *w = allSubLayouts.at(i)->itemAt(j)->widget(); if(w != NULL) { w->setVisible(false); } } } }
Since I knew that there were layouts available, instead of using
findChildren()
I usedallSubLayouts.at(i)->*itemAt*(j)->*widget*();
While this solves my problem of widgets and layouts overlapping in MainWindow, I don't know if it will cause a memory leak or will use up too much memory, since I'm not deleting any widgets/layouts.
-
Hi
"allSubLayouts.at(i)->*itemAt*(j)->*widget*();"
made my brain hurt :)
Well since u are not deleting any widgets, u will use more and more memory,
but since they are still owned by layout, it will be free when application ends.