Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGridLayout's row is not removed
Forum Updated to NodeBB v4.3 + New Features

QGridLayout's row is not removed

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 435 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • napajejenunedk0N Offline
    napajejenunedk0N Offline
    napajejenunedk0
    wrote on last edited by
    #1

    I have a QGridLayout that has several rows each of which having two widgets. Removing both widgets in any given row doesn't reduce the QGridLayout::rowCount(). Tried removing the widgets using both:

    1. QLayout::removeWidget
    2. QLayout::removeItem - I was thinking that QGridLayout could somehow be still keeping the QLayoutItem despite the fact that I've removed the widget using 1. but no

    Any ideas why the layout behaves this way?

    JonBJ 1 Reply Last reply
    0
    • H Offline
      H Offline
      Harsha P
      wrote on last edited by
      #2

      The reason QGridLayout::rowCount() does not decrease when removing widgets is that QGridLayout internally maintains its structure even after widgets are removed. Removing a widget using removeWidget() or removeItem() only detaches the widget but does not remove the row itself.

      void removeRow(QGridLayout* layout, int row) {
          for (int col = layout->columnCount() - 1; col >= 0; --col) {
              QLayoutItem* item = layout->itemAtPosition(row, col);
              if (item) {
                  layout->removeItem(item);
                  if (QWidget* widget = item->widget()) {
                      widget->hide();  
                      layout->removeWidget(widget);
                      delete widget;  
                  }
                  delete item;  
              }
          }
      }
      
      
      1 Reply Last reply
      0
      • napajejenunedk0N napajejenunedk0

        I have a QGridLayout that has several rows each of which having two widgets. Removing both widgets in any given row doesn't reduce the QGridLayout::rowCount(). Tried removing the widgets using both:

        1. QLayout::removeWidget
        2. QLayout::removeItem - I was thinking that QGridLayout could somehow be still keeping the QLayoutItem despite the fact that I've removed the widget using 1. but no

        Any ideas why the layout behaves this way?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @napajejenunedk0
        Removing items/widgets does not reduce the rows or columns in a QGridLayout, even if a row/column becomes empty.
        There are several posts on this forum (and elsewhere) to the effect that you cannot delete a row/column from the QGridLayout itself.
        I think the best is @kshegunov's https://forum.qt.io/post/343288

        How to enforce QGridLayout to delete rows that do not contain any Widgets anymore?

        You can't, you have to manually shift up (left) all cells below (right).

        Also https://stackoverflow.com/a/13406780/489865

        The issue is that QGridLayout::rowCount() doesn't actually return the number of rows that you can see, it actually returns the number of rows that QGridLayout has internally allocated for rows of data (yes, this isn't very obvious and isn't documented).

        To get around this you can either delete the QGridLayout and recreate it

        Or https://www.qtcentre.org/threads/49669-Remove-column-from-QGridLayout?p=292883#post292883

        The grid layout doesn't have any methods to remove cells, so if you need them removed you need to recreate the layout,

        Alternatively you can move all content below the cleared row upwards so that empty rows are at the end and then reuse those when adding new items.

        1 Reply Last reply
        0
        • napajejenunedk0N Offline
          napajejenunedk0N Offline
          napajejenunedk0
          wrote on last edited by
          #4

          As I was expecting. Thanks a lot for your answers. You mean that even if I delete all UI elements in row 3 out of 4 total, still rows 1, 2, 4 would contain layout items but not 3? And QGridLayout wouldn't move row 4 layout items to row 3 despite the fact that it would keep the maximum allocated rows count to 4?

          JonBJ 1 Reply Last reply
          0
          • napajejenunedk0N napajejenunedk0

            As I was expecting. Thanks a lot for your answers. You mean that even if I delete all UI elements in row 3 out of 4 total, still rows 1, 2, 4 would contain layout items but not 3? And QGridLayout wouldn't move row 4 layout items to row 3 despite the fact that it would keep the maximum allocated rows count to 4?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @napajejenunedk0 Yes. QGridLayout is not going to do any row/column or content moving.

            1 Reply Last reply
            0
            • napajejenunedk0N Offline
              napajejenunedk0N Offline
              napajejenunedk0
              wrote on last edited by
              #6

              @JonB, @Harsha-P, one last question:

              1. Add widgets in 3 rows.
              2. Remove all widgets from 2nd row.
              3. Add widgets to 3rd row - would QGridView add them as third items or would replace the previous 3rd row items?
              JonBJ Pl45m4P 3 Replies Last reply
              0
              • napajejenunedk0N napajejenunedk0

                @JonB, @Harsha-P, one last question:

                1. Add widgets in 3 rows.
                2. Remove all widgets from 2nd row.
                3. Add widgets to 3rd row - would QGridView add them as third items or would replace the previous 3rd row items?
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @napajejenunedk0
                I don't know whether it adds multiple widgets to one cell or replaces the widget there, though the fact that you have emptied out the second row should not be relevant. It will not have moved the previous third row's widgets down to the second row, as we discussed earlier. For this why don't you actually try code, it's quicker than asking and you would know for sure....

                1 Reply Last reply
                1
                • napajejenunedk0N napajejenunedk0

                  @JonB, @Harsha-P, one last question:

                  1. Add widgets in 3 rows.
                  2. Remove all widgets from 2nd row.
                  3. Add widgets to 3rd row - would QGridView add them as third items or would replace the previous 3rd row items?
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @napajejenunedk0
                  By the way, going back to your question about removing widgets. You might want to read through the somewhat detailed https://stackoverflow.com/a/19256990/489865 for your information. Although it confirms you cannot alter what rows/columns are in the QGridLayout itself, it does show suggested code when emptying a row to keep it as small as possible, viz.

                      layout->setRowMinimumHeight(row, 0);
                      layout->setRowStretch(row, 0);
                  

                  which you might want, I don't know?

                  1 Reply Last reply
                  0
                  • napajejenunedk0N napajejenunedk0

                    @JonB, @Harsha-P, one last question:

                    1. Add widgets in 3 rows.
                    2. Remove all widgets from 2nd row.
                    3. Add widgets to 3rd row - would QGridView add them as third items or would replace the previous 3rd row items?
                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #9

                    @napajejenunedk0 said in QGridLayout's row is not removed:

                    Add widgets to 3rd row - would QGridView add them as third items or would replace the previous 3rd row items?

                    You can't just "add" widgets to a QGridLayout. You assign a position in its grid... so as long as you don't add to populated cells, it will append the widget to the grid (and extend the grid).
                    As @JonB said, whether you delete or modify other rows, does not matter. The rows/cols/cells are still there logically but empty and don't show anything unless you have specified some huge margin/offset.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved