Click to See Complete Forum and Search --> : Question on QT


tecknophreak
07-16-2005, 03:08 PM
Let's say I have a qvboxlayout, and add a qhboxlayout with a bunch of widgets, one being a button with a slot. In said slot, if I do a vbox->removeItem(hbox) then delete hbox. How do I get rid of all the children of the hbox?

To quote the QT reference:
"remove Item
Removes the layout item item from the layout. It is the caller's responsibility to delete the item.

Notice that item can be a layout (since QLayout inherits QLayoutItem). "

This doesn't say that I have to delete the children or not.

If I go ahead and delete each of the internal widgets, it works, but this means I have to keep a pointer to each of the widgets. Depending on the application I'll be running in the future, this might be a bunch of widgets(not to mention a real pain).

Of course I could add a vector of widgets which go in the box to my class, but that just seems ugly.

This all coming from a guy who's been working with GTK for a few years now and is used to gtk_widget_destory(widget) cleaning up all the mess for me. ;)

bwkaz
07-16-2005, 07:37 PM
See the documentation here:

http://doc.trolltech.com/4.0/qlayout.html#takeAt

which gives "a safe way to remove all items from a layout". ;)

tecknophreak
07-17-2005, 11:31 AM
Sure, works great if you've got 4.0. :p However, FC4 is even still at 3.3. The easy way out is to just upgrade to 4.0. :rolleyes:

bwkaz
07-17-2005, 01:25 PM
Well... maybe start here:

http://doc.trolltech.com/3.3/qlayout.html

, use the ->iterator() function, and do something like:

QLayoutItem *item;
QLayoutIterator *iter = vbox->iterator();

while((item = iter->takeCurrent()) != 0) {
// do whatever with item
delete item;
}

tecknophreak
07-17-2005, 01:41 PM
Ohhh, right. Thanks, just getting started with the QT and having a little problem moving over from GTK in a few days. You wouldn't happen to know how to tell what type of widget is, would you? Something like the GTK IS_HBOX(widget)-type.

tecknophreak
07-17-2005, 03:52 PM
Ok, well the above didn't work out quite the way I had wanted, but after looking around at the QT reference doc, LayoutItems have a widget() function. So the modified source should look like:

while((item = it.takeCurrent()) != 0) {
// do whatever with item
delete item->widget();
}

If you just do the delete item, it doesn't delete the item, just the LayoutItem.

tecknophreak
07-17-2005, 06:19 PM
Ok, so I found out the isA function for QObjects, but it turned out I didn't need it for what I was looking for. I just checked to see if a child was a QLayout and then I got a recursive function for clearing a QLayout:

void clearLayout(QLayout *l) {
QLayoutItem *item;
QLayoutIterator it = l->iterator();

while((item = it.takeCurrent()) != 0) {

if (item->layout()) {
clearLayout((QLayout *)item->layout());
}
topVbox->remove(item->widget());
delete item->widget();
}
}

bwkaz
07-18-2005, 09:12 PM
Ah, yeah, that looks like it should work.

There's also a C++ facility, called "run-time type information" or RTTI. It has something to do with the __typeof or typeof operator, or something. It's been way too long since I used it last, so I don't remember how it works anymore, but searching for RTTI and/or typeof might shed some light.