PDA

View Full Version : remove widgets from layout in run time



bindigi01
5th June 2009, 14:08
Hi
i am bulding an application that needs to put image thumbnails in QHBoxLayout
i creant new widget and put image on it and add it to layout
this is OK

the Q? is i need to remove this widget from lay out in run time ..HOW?
i used

Layout->removeItem()
but it dosent work

this is my code snippist to add layout RUN OK



m_thumbnailsWidgetHLayout = new QHBoxLayout(ui.thumbnailsScrollAreaWidgetContents) ;

ThumbnailWidget* thumb = new ThumbnailWidget;
QPixmap scaled = pixmap.scaled(ThumbnailWidget::THUMBNAIL_SIZE,
ThumbnailWidget::THUMBNAIL_SIZE,
Qt::KeepAspectRatioByExpanding,
Qt::FastTransformation);
thumb->setPixmap(scaled);
thumb->setThumbnailIndex(key);
m_thumbnailsWidgetHLayout->addWidget(thumb);



and this is the code of removing widgets NOT RUN OK


for (int i = 0; i < m_thumbnailsWidgetHLayout->count(); i++) {
m_thumbnailsWidgetHLayout->removeItem(m_thumbnailsWidgetHLayout->itemAt(i));
}


thanks

Lykurg
5th June 2009, 14:16
What exactly runs not? Remember that you have to delete the widgets yourself! So maybe it's better to use QObject::children () with QLayout::removeWidget() (plus an appropriate cast) and delete.

bindigi01
5th June 2009, 14:31
I modefied my code and used this

QList<QObject*> objlist = m_thumbnailsWidgetHLayout->children();
for (int i = 0; i < objlist.count(); i++) {
ThumbnailWidget* thumb = (ThumbnailWidget*) objlist.at(i);
m_thumbnailsWidgetHLayout->removeWidget(thumb);
delete thumb;
}
but old widgets still in ui and when i add new thumbnail widgets it continue add to lay out
for example first time i have 3 thumbs
second i removed the first 3 and add 5
my result is 8 thumb in the scrollarea

Lykurg
5th June 2009, 14:37
but old widgets still in ui and when i add new thumbnail widgets it continue add to lay out
for example first time i have 3 thumbs
second i removed the first 3 and add 5
my result is 8 thumb in the scrollarea

That's wired, does thumb->hide() "removes" the pictures from your layout? debug the values, are they pointing to the right elements?

Lykurg
5th June 2009, 14:44
I just remember better use something like

QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();
then you have not to cast and your pointers are valid

And where did you set the layout m_thumbnailsWidgetHLayout? The layout is for which widget?

bindigi01
5th June 2009, 18:42
i set the layout in the constructor

Lykurg
5th June 2009, 19:31
i set the layout in the constructor

What class is ui.thumbnailsScrollAreaWidgetContents? And please post the whole function where you remove the old and set the new images. Only so we can find your error because the code is right.

bindigi01
5th June 2009, 19:45
What class is ui.thumbnailsScrollAreaWidgetContents? the widget inside the scroll area


void ViewerMainWindow::fillThumbnails() {
if (FrameTile::activeFrameTile != 0) {
FrameTile* frameTile = FrameTile::activeFrameTile;

dicom::ImageIODFacade * fcd = frameTile->getImgFcd();
if (fcd != 0) {

QList<QObject*> objlist = m_thumbnailsWidgetHLayout->children();
for (int i = 0; i < objlist.count(); i++) {
ThumbnailWidget* thumb = (ThumbnailWidget*) objlist.at(i);
m_thumbnailsWidgetHLayout->removeWidget(thumb);
delete thumb;
}

QMap<size_t, QPixmap> map = fcd->getThumpnails();

foreach(size_t key, map.keys()) {
QPixmap pixmap = map.value(key);
ThumbnailWidget* thumb = new ThumbnailWidget;
QPixmap scaled = pixmap.scaled(ThumbnailWidget::THUMBNAIL_SIZE,
ThumbnailWidget::THUMBNAIL_SIZE,
Qt::KeepAspectRatioByExpanding,
Qt::FastTransformation);
thumb->setPixmap(scaled);
thumb->setThumbnailIndex(key);
m_thumbnailsWidgetHLayout->addWidget(thumb);
}
}
}
}

Lykurg
5th June 2009, 19:54
Ok, sorry, I don't have a clue. The code seems right. The only thing I can guess is that fcd->getThumpnails() is still returning the old pictures. You want run following simple extension-debug-variant of your function:


void ViewerMainWindow::fillThumbnails() {
if (FrameTile::activeFrameTile != 0) {
FrameTile* frameTile = FrameTile::activeFrameTile;

dicom::ImageIODFacade * fcd = frameTile->getImgFcd();
if (fcd != 0) {

QList<QObject*> objlist = m_thumbnailsWidgetHLayout->children();
qWarning() << "Object count" << objlist.count(); //DEBUG
for (int i = 0; i < objlist.count(); i++) {
ThumbnailWidget* thumb = (ThumbnailWidget*) objlist.at(i);
m_thumbnailsWidgetHLayout->removeWidget(thumb);
delete thumb;
}
qWarning() << "Object count (after)" << m_thumbnailsWidgetHLayout->children().count(); //DEBUG

QMap<size_t, QPixmap> map = fcd->getThumpnails();

foreach(size_t key, map.keys()) {
qWarning() << "add" << key; //DEBUG
QPixmap pixmap = map.value(key);
ThumbnailWidget* thumb = new ThumbnailWidget;
QPixmap scaled = pixmap.scaled(ThumbnailWidget::THUMBNAIL_SIZE,
ThumbnailWidget::THUMBNAIL_SIZE,
Qt::KeepAspectRatioByExpanding,
Qt::FastTransformation);
thumb->setPixmap(scaled);
thumb->setThumbnailIndex(key);
m_thumbnailsWidgetHLayout->addWidget(thumb);
}
}
}
}

What's the output?

bindigi01
6th June 2009, 21:42
i have triggered called this function many times
here is the outPut

############first 16 thumb
count Before 0
count after 0
add key 1
add key 2
add key 3
add key 4
add key 5
add key 6
add key 7
add key 8
add key 9
add key 10
add key 11
add key 12
add key 13
add key 14
add key 15
add key 16

############first 16 thumb again
count Before 0
count after 0
add key 1
add key 2
add key 3
add key 4
add key 5
add key 6
add key 7
add key 8
add key 9
add key 10
add key 11
add key 12
add key 13
add key 14
add key 15
add key 16

############ another image with 1 thumb
count Before 0
count after 0
add key 1

bindigi01
6th June 2009, 23:36
solved :D thanks Lykurg for helping

here is the code that solvs



int i = 0;
QLayoutItem *child;
while ((child = m_thumbnailsWidgetHLayout->takeAt(0)) != 0) {
delete child->widget();
delete child;
}