remove widgets from layout in run time
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
Code:
Layout->removeItem()
but it dosent work
this is my code snippist to add layout RUN OK
Code:
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
Code:
for (int i = 0; i < m_thumbnailsWidgetHLayout->count(); i++) {
m_thumbnailsWidgetHLayout->removeItem(m_thumbnailsWidgetHLayout->itemAt(i));
}
thanks
Re: remove widgets from layout in run time
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.
Re: remove widgets from layout in run time
I modefied my code and used this
Code:
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
Re: remove widgets from layout in run time
Quote:
Originally Posted by
bindigi01
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?
Re: remove widgets from layout in run time
I just remember better use something like
Code:
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?
Re: remove widgets from layout in run time
i set the layout in the constructor
Re: remove widgets from layout in run time
Quote:
Originally Posted by
bindigi01
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.
Re: remove widgets from layout in run time
What class is ui.thumbnailsScrollAreaWidgetContents? the widget inside the scroll area
Code:
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()) {
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);
}
}
}
}
Re: remove widgets from layout in run time
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:
Code:
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
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?
Re: remove widgets from layout in run time
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
Re: remove widgets from layout in run time
solved :D thanks Lykurg for helping
here is the code that solvs
Code:
int i = 0;
while ((child = m_thumbnailsWidgetHLayout->takeAt(0)) != 0) {
delete child->widget();
delete child;
}