Results 1 to 11 of 11

Thread: remove widgets from layout in run time

  1. #1
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face 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
    Qt Code:
    1. Layout->removeItem()
    To copy to clipboard, switch view to plain text mode 
    but it dosent work

    this is my code snippist to add layout RUN OK

    Qt Code:
    1. m_thumbnailsWidgetHLayout = new QHBoxLayout(ui.thumbnailsScrollAreaWidgetContents);
    2.  
    3. ThumbnailWidget* thumb = new ThumbnailWidget;
    4. QPixmap scaled = pixmap.scaled(ThumbnailWidget::THUMBNAIL_SIZE,
    5. ThumbnailWidget::THUMBNAIL_SIZE,
    6. Qt::KeepAspectRatioByExpanding,
    7. Qt::FastTransformation);
    8. thumb->setPixmap(scaled);
    9. thumb->setThumbnailIndex(key);
    10. m_thumbnailsWidgetHLayout->addWidget(thumb);
    To copy to clipboard, switch view to plain text mode 

    and this is the code of removing widgets NOT RUN OK
    Qt Code:
    1. for (int i = 0; i < m_thumbnailsWidgetHLayout->count(); i++) {
    2. m_thumbnailsWidgetHLayout->removeItem(m_thumbnailsWidgetHLayout->itemAt(i));
    3. }
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  3. #3
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: remove widgets from layout in run time

    I modefied my code and used this
    Qt Code:
    1. QList<QObject*> objlist = m_thumbnailsWidgetHLayout->children();
    2. for (int i = 0; i < objlist.count(); i++) {
    3. ThumbnailWidget* thumb = (ThumbnailWidget*) objlist.at(i);
    4. m_thumbnailsWidgetHLayout->removeWidget(thumb);
    5. delete thumb;
    6. }
    To copy to clipboard, switch view to plain text mode 
    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

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: remove widgets from layout in run time

    Quote Originally Posted by bindigi01 View Post
    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: remove widgets from layout in run time

    I just remember better use something like
    Qt Code:
    1. QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();
    To copy to clipboard, switch view to plain text mode 
    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?

  6. #6
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: remove widgets from layout in run time

    i set the layout in the constructor

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: remove widgets from layout in run time

    Quote Originally Posted by bindigi01 View Post
    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.

  8. #8
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: remove widgets from layout in run time

    What class is ui.thumbnailsScrollAreaWidgetContents? the widget inside the scroll area

    Qt Code:
    1. void ViewerMainWindow::fillThumbnails() {
    2. if (FrameTile::activeFrameTile != 0) {
    3. FrameTile* frameTile = FrameTile::activeFrameTile;
    4.  
    5. dicom::ImageIODFacade * fcd = frameTile->getImgFcd();
    6. if (fcd != 0) {
    7.  
    8. QList<QObject*> objlist = m_thumbnailsWidgetHLayout->children();
    9. for (int i = 0; i < objlist.count(); i++) {
    10. ThumbnailWidget* thumb = (ThumbnailWidget*) objlist.at(i);
    11. m_thumbnailsWidgetHLayout->removeWidget(thumb);
    12. delete thumb;
    13. }
    14.  
    15. QMap<size_t, QPixmap> map = fcd->getThumpnails();
    16.  
    17. foreach(size_t key, map.keys()) {
    18. QPixmap pixmap = map.value(key);
    19. ThumbnailWidget* thumb = new ThumbnailWidget;
    20. QPixmap scaled = pixmap.scaled(ThumbnailWidget::THUMBNAIL_SIZE,
    21. ThumbnailWidget::THUMBNAIL_SIZE,
    22. Qt::KeepAspectRatioByExpanding,
    23. Qt::FastTransformation);
    24. thumb->setPixmap(scaled);
    25. thumb->setThumbnailIndex(key);
    26. m_thumbnailsWidgetHLayout->addWidget(thumb);
    27. }
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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:

    Qt Code:
    1. void ViewerMainWindow::fillThumbnails() {
    2. if (FrameTile::activeFrameTile != 0) {
    3. FrameTile* frameTile = FrameTile::activeFrameTile;
    4.  
    5. dicom::ImageIODFacade * fcd = frameTile->getImgFcd();
    6. if (fcd != 0) {
    7.  
    8. QList<QObject*> objlist = m_thumbnailsWidgetHLayout->children();
    9. qWarning() << "Object count" << objlist.count(); //DEBUG
    10. for (int i = 0; i < objlist.count(); i++) {
    11. ThumbnailWidget* thumb = (ThumbnailWidget*) objlist.at(i);
    12. m_thumbnailsWidgetHLayout->removeWidget(thumb);
    13. delete thumb;
    14. }
    15. qWarning() << "Object count (after)" << m_thumbnailsWidgetHLayout->children().count(); //DEBUG
    16.  
    17. QMap<size_t, QPixmap> map = fcd->getThumpnails();
    18.  
    19. foreach(size_t key, map.keys()) {
    20. qWarning() << "add" << key; //DEBUG
    21. QPixmap pixmap = map.value(key);
    22. ThumbnailWidget* thumb = new ThumbnailWidget;
    23. QPixmap scaled = pixmap.scaled(ThumbnailWidget::THUMBNAIL_SIZE,
    24. ThumbnailWidget::THUMBNAIL_SIZE,
    25. Qt::KeepAspectRatioByExpanding,
    26. Qt::FastTransformation);
    27. thumb->setPixmap(scaled);
    28. thumb->setThumbnailIndex(key);
    29. m_thumbnailsWidgetHLayout->addWidget(thumb);
    30. }
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    What's the output?

  10. The following user says thank you to Lykurg for this useful post:

    bindigi01 (6th June 2009)

  11. #10
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  12. #11
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: remove widgets from layout in run time

    solved thanks Lykurg for helping

    here is the code that solvs

    Qt Code:
    1. int i = 0;
    2. QLayoutItem *child;
    3. while ((child = m_thumbnailsWidgetHLayout->takeAt(0)) != 0) {
    4. delete child->widget();
    5. delete child;
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 14th May 2009, 22:00
  2. Custom widgets in layout
    By Palmik in forum Newbie
    Replies: 11
    Last Post: 26th January 2009, 12:08
  3. Promoted widgets and layout boxes
    By notsonerdysunny in forum Qt Tools
    Replies: 3
    Last Post: 2nd May 2007, 14:15
  4. Replies: 3
    Last Post: 25th March 2006, 06:15
  5. mouseTracking in two widgets at the same time?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 30th January 2006, 17:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.