Results 1 to 9 of 9

Thread: Delete single widget from QLayoutGrid

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Delete single widget from QLayoutGrid

    Hi there,

    I have been trying to delete a widget from a grid layout all morning without success. Either the widget is still there or the program crashes. Can someone tell me how this should be done? The widgets are added using addWidget(cardVector[k], k/4, k%4);

    I have a grid layout with two rows and four columns containing different cards. I would like to delete one of these cards at any position and want that the other cards move up and that an empty "field" is added at the end. How do I do that?

    Thanks for taking a look!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete single widget from QLayoutGrid

    Since you also want to reposition other widgets, the easiest way is to delete the layout and create a new one and only add those widgets you want at the position you want to be.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Delete single widget from QLayoutGrid

    I thought about that too but the grid layout was placed in Creator and is part of another layout. If I delete it I do not know how to put a new one back at the same position. Currently I can access the layout using ui->cardLayout but it is the first object of main layout. If I recreate it I need to be able to access as ui->cardLayout again otherwise I need to rewrite most of my code...

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete single widget from QLayoutGrid

    In such a case it is handy to have a widget as a container in the place you currently have the just the layout.

    I.e. instead of nesting the grid layout in another, put a plain widget into the outer layout and use a grid on the widget.
    Then you can easily just recreate the grid layout for that "container".

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Delete single widget from QLayoutGrid

    Sorry but I still cannot crack it. I went with your advise and created a Widget in Creator and gave it the size I want the card area to have. In the code I initialize the cards:
    Qt Code:
    1. cardVector.resize(8);
    2.  
    3. QGridLayout *grid = new QGridLayout;
    4. grid->setRowMinimumHeight(0,240);
    5. grid->setRowMinimumHeight(1,240);
    6. int k = 0;
    7. for(int i = 0; i < 2; ++i){
    8. for(int j = 0; j < 4; ++j){
    9. cardVector[k] = new Card;
    10. cardVector[k]->index = k;
    11. [...]
    12. grid->addWidget(cardVector[k], i, j);
    13. k++;
    14. }
    15. }
    16. ui->cardContainer->setLayout(grid);
    To copy to clipboard, switch view to plain text mode 
    This works fine but redoing it does not work.
    Qt Code:
    1. cards.removeAt(activeIndex);
    2. cardIndex--;
    3. cardVector.removeAt(activeIndex);
    4.  
    5. int k = 7;
    6. cardVector.resize(8);
    7. cardVector[k] = new Card;
    8. [...]
    9.  
    10. // redoing the vector
    11. for(int k = 0; k < fighterIndex; ++k){
    12. card[k].name = card[k].type+" "+QString::number(k+1);
    13. cardVector[k]->setTitle(card[k].name);
    14. updateCard(k);
    15. }
    16.  
    17. // Redoing the layout
    18. QLayoutItem* item;
    19. while((item = ui->cardContainer->layout()->takeAt(0)) != NULL ){
    20. delete item->widget();
    21. delete item;
    22. }
    23. delete ui->cardContainer->layout();
    24.  
    25. QGridLayout *grid = new QGridLayout;
    26. grid->setRowMinimumHeight(0,240);
    27. grid->setRowMinimumHeight(1,240);
    28.  
    29. k = 0;
    30. for(int i = 0; i< 2; ++i){
    31. for(int j = 0; j<4;++j){
    32. cardVector[k]->index = k;
    33. grid->addWidget(cardVector[k], i, j); // <- at this line the program crashes
    34. k++;
    35. }
    36. }
    37. ui->cardContainer->setLayout(grid);
    38. }
    To copy to clipboard, switch view to plain text mode 
    I do not see what is different from the first part. Why does the program crash when I add a widget to the layout here? The code is identical to the first one, isn't it? What can I do to find the problem?

    Thanks for the help!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete single widget from QLayoutGrid

    You have a while loop that deletes all widgets.
    Then you try to add the now danlging pointers to the layout.

    Since you always seem to have 8 cards (even the second code block has cardVector.resize(8)), why delete at all?
    My initial suggestion was based on the assmuption that "deleting" means you have one less widget than before.

    Cheers,
    _

  7. #7
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Delete single widget from QLayoutGrid

    I tried to rewrite them without deleting them first. Then I ended up with the "new" cards on top of the old cards.
    Actually I only need to update the view. Adjusting the two vectors with the information is easy, but I do not get the view updated. =(

    What do you mean with dangling pointers? I thought I have deleted them properly to start from scratch.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete single widget from QLayoutGrid

    Quote Originally Posted by KeineAhnung View Post
    I tried to rewrite them without deleting them first. Then I ended up with the "new" cards on top of the old cards.
    If you don't delete you don't have to create new cards, right?

    Quote Originally Posted by KeineAhnung View Post
    Actually I only need to update the view. Adjusting the two vectors with the information is easy, but I do not get the view updated. =(
    Do you call the respective setters on each card so that it is filled with new new data?

    Quote Originally Posted by KeineAhnung View Post
    What do you mean with dangling pointers? I thought I have deleted them properly to start from scratch.
    You delete all widgets in the layout here in a while loop, but you do not clear the cardVector. So all pointers in cardVector are now "dangling" (point to some memory that no longer holds a Card widget).
    You then take these pointers and try to add them to the layout again.

    As I suggested in comment #2 it would be easier to just deleted the layout and re-add the widgets you want at the position you want, no need to delete or create any widgets.

    Obviously filling the existing and layouted widgets with new data is even better, but your initial request didn't say that this was on option.

    Cheers,
    _

  9. #9
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Delete single widget from QLayoutGrid

    Actually it is not an option. Refilling did not work. The connection between the cardVector and the widget is so strong that even after shortening the vector the connection still existed. So after deleting a card and adding a new one the newest one would still be added on the next position and not on top of the last one.

    I finally got it to work by destroying the cardVector as well and und redoing it.

    Thanks for your help _!

Similar Threads

  1. Replies: 2
    Last Post: 19th July 2013, 17:24
  2. [QTextEdit] Delete single lines
    By bmn in forum Qt Programming
    Replies: 3
    Last Post: 18th July 2012, 17:57
  3. Replies: 2
    Last Post: 15th March 2012, 08:48
  4. Replies: 4
    Last Post: 2nd December 2010, 07:51
  5. delete a single file from a directory
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 12th March 2009, 06:46

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.