Results 1 to 9 of 9

Thread: rowCount does not update after removing widgets from grid layout

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default rowCount does not update after removing widgets from grid layout

    I'm trying to dynamically remove/delete rows from a QGridLayout, but I can't seem to get the rowCount() property to update. This is what I'm trying:

    Qt Code:
    1. qDebug()<<"Before: "<<my_grid_layout->rowCount();
    2.  
    3. QLayoutItem *child = my_grid_layout->takeAt(row_index);
    4.  
    5. my_grid_layout->removeItem(child);
    6. my_grid_layout->removeWidget(child->widget());
    7.  
    8. delete child->widget();
    9. delete child;
    10.  
    11. my_grid_layout->update();
    12.  
    13. qDebug()<<"After: "<<my_grid_layout->rowCount();
    To copy to clipboard, switch view to plain text mode 

  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: rowCount does not update after removing widgets from grid layout

    if you use takeAt() you don't have to call removeItem() and removeWidget(). About the row count I guess it is only increasing not decreasing. But I am not sure about it right now.

  3. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: rowCount does not update after removing widgets from grid layout

    Thanks for the reply. I suppose I'll just have to keep track of the row count myself. The rowCount() property is basically useless if it only increases!

  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: rowCount does not update after removing widgets from grid layout

    Well it really only increases. But also consider following:
    Qt Code:
    1. QWidget *w = new QWidget;
    2. l.addWidget(w, 3, 0);
    3. qWarning() << l.rowCount();
    To copy to clipboard, switch view to plain text mode 
    it will return 4. So the row property is not what you are expecting!
    And do you need the grid functionality because a QVBoxLayout should work like you expect it.

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

    Ishmael (3rd June 2010)

  6. #5
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: rowCount does not update after removing widgets from grid layout

    Actually, in your example, I think rowCount() returns exactly what you would expect. You added a widget to the 4th row (index 3) so it returns 4. The problem is, if you remove that same widget, it still says 4! Anyway, your suggestion to use a QVBoxLayout is a good one. I'll try that. Thanks!

  7. #6
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rowCount does not update after removing widgets from grid layout

    You added a widget to the 4th row (index 3) so it returns 4. The problem is, if you remove that same widget, it still says 4!
    there may be more than one item in the row, so deleting it just because an item was removed is probably not a good idea!
    Got to keep the loonies on the path ...

  8. #7
    Join Date
    May 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: rowCount does not update after removing widgets from grid layout

    But this behavior is a bug, or at least incredibly non-intuitive. If I add a single widget at row 3, then delete the only widget in that row, the row count should most certainly not include that row. As it is right now, is is impossible to reduce the size of a grid if at any point an item was added to a row.

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: rowCount does not update after removing widgets from grid layout

    What do you propose it does if there a full rows of widget at 0, 1, 2, and 4 and only one widget in row 3, which you then remove? Should row three be removed and the widgets in row four be renumbered and the layout resized? How about if taking out cell in row 3 also makes its column empty? Remove it? Doing this automatically potentially breaks any code that references the grid cells by row and column.

  10. #9
    Join Date
    May 2021
    Posts
    1
    Qt products
    Platforms
    Windows

    Default Re: rowCount does not update after removing widgets from grid layout

    If it suits you, you can rather use my_grid_layout->count() and divide it by the number of columns in the layout if the number of columns is known and not going to change.

Similar Threads

  1. Grid Layout problem
    By succorguy in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2009, 00:01
  2. How to update scene after removing items
    By nileshsince1980 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 10:56
  3. How do I layout in a Grid?
    By DPinLV in forum Qt Tools
    Replies: 7
    Last Post: 10th August 2006, 02:37
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 13:45
  5. Grid layout
    By nupul in forum Qt Programming
    Replies: 7
    Last Post: 21st April 2006, 22:15

Tags for this Thread

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.