Results 1 to 5 of 5

Thread: remove item for QGirdLayout doesn't work.

  1. #1
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default remove item for QGirdLayout doesn't work.

    I'm trying to dynamically create QComboBoxes and QLabels during run time based on the users input. Example: if the users enters 3, my program displays 3 comobboxes and 3 labels. If they then type in 2, my program will then display only 2 comboboxes and 2 labels. When ever the user changes the number of comboboxes and labels this function is called (numOfSurfaces is the user input that the number of comboboxes and labels depend on):

    Qt Code:
    1. for(int row=0; row<surf_typeGrid->rowCount(); row++)
    2. {
    3. surf_typeGrid->removeItem(surf_typeGrid->itemAtPosition(row,0));
    4. surf_typeGrid->removeItem(surf_typeGrid->itemAtPosition(row,1));
    5. }
    6.  
    7. int numOfSurfaces = (nwall_typeLine->displayText()).toInt();
    8. for(int i=1; i<=numOfSurfaces; i++)
    9. {
    10. char surfLabel [15];
    11. sprintf(surfLabel, "Surface %d", i);
    12. surf_typeGrid->addWidget(new QLabel(surfLabel), i-1, 0);
    13. surf_typeGrid->addWidget(new QComboBox(), i-1,1);
    14. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that when the user changes the number of comboboxes and labels, instead of deleting the old comboboxes and lables and displaying just the new ones, the program still displays the old comboxes and labels and just puts the new ones on top of them. How do I make sure the old comboboxes and labels get removed?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: remove item for QGirdLayout doesn't work.

    From QLayout::removeItem() docs:
    Removes the layout item item from the layout. It is the caller's responsibility to delete the item.
    In other words, QLayout::removeItem() removes the item from the layout in the sense that the geometry of the item is no more managed by the layout but that's all. The item (nor the widget) is not deleted.
    J-P Nurmi

  3. #3
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: remove item for QGirdLayout doesn't work.

    You're exactly right. I wasn't actually deleting them. When I realized this, I figured it out. Being new to c++, I'm evidently struggling with pointers. Here's my solution in case anyone else needs it.
    Qt Code:
    1. void SurfCntrlParams::confSurf_typeInputLayout(const QString & text)
    2. {
    3. std::vector<QComboBox*>::iterator it = surf_typeBoxVector->begin();
    4. std::vector<QLabel*>::iterator it2 = surf_typeLabelVector->begin();
    5.  
    6. for(;it<(surf_typeBoxVector->end()); it++, it2++)
    7. {
    8. delete *it2;
    9. delete *it;
    10. }
    11.  
    12. surf_typeBoxVector->clear();
    13. surf_typeLabelVector->clear();
    14.  
    15. int numOfSurfaces = text.toInt();
    16. for(int i=1; i<=numOfSurfaces; i++)
    17. {
    18. char surfLabel [15];
    19. sprintf(surfLabel, "Surface %d", i);
    20. surf_typeBoxVector->push_back(new QComboBox());
    21. (surf_typeBoxVector->back())->addItems(*surf_typeList);
    22. surf_typeLabelVector->push_back(new QLabel(surfLabel));
    23. }
    24.  
    25. it = surf_typeBoxVector->begin();
    26. it2 = surf_typeLabelVector->begin();
    27. for(int row2=0;it<(surf_typeBoxVector->end()); row2++,it++, it2++)
    28. {
    29. surf_typeGrid->addWidget(*it2,row2,0);
    30. surf_typeGrid->addWidget(*it,row2,1);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: remove item for QGirdLayout doesn't work.

    Have you considered using model-view approach..?
    J-P Nurmi

  5. #5
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: remove item for QGirdLayout doesn't work.

    Wow, that model view thing looks like it might just be exactly what the doctor ordered. Wow. Thanks a bunch jpn. I'll have to look into that more.

Similar Threads

  1. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  2. QListWidgetItem remove an item
    By mattia in forum Newbie
    Replies: 4
    Last Post: 9th November 2007, 07:57
  3. Replies: 2
    Last Post: 1st August 2006, 10:23

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.