Results 1 to 8 of 8

Thread: How to set fixed row and column number in QGridLayout

  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to set fixed row and column number in QGridLayout

    I'm trying to add widgets in QGridLayout with fixed row number and column number, so a widget will always on the same location regardless of the other rows or columns are empty or not.
    For example, I want to make a layout with 2 rows and 3 columns.
    When I have 5 widgets, it always looks like
    111.png
    However, if I only have 2 widgets, I want it looks like
    112.png
    But NOT looks like
    113.png
    which is default by QGridLayout because I cannot specify the row and column number until I add a widget on that row/column.

    Right now I found few ways to do it.
    One Way is always add a dummy widget at position x_max, y_max, which are number of row and column, and remove it when a real widget need to be placed.

    Another way is change the alignment not to be center, but then I have a lot margin settings need to be manually calculated because I want each widget still centered in its position.

    Do you have any better way to do it? Thanks in advance.

  2. #2
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set fixed row and column number in QGridLayout

    Anyone has ideas? I think this should be a general requirements (like the layout in android and ios). I will be surprised if Qt doesn't have a intuitive way to do it.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set fixed row and column number in QGridLayout

    Layouts are designed to be dynamic and change their configuration as their content changes. So the grid layout is doing what it is supposed to do as you add widgets to it. If you want to force a given row x cell count, then you should probably add QSpacerItem instances to at least one of each of the left / right and top / bottom edge cells as placeholders to force the rest of the cells to the upper left.

  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: How to set fixed row and column number in QGridLayout

    Quote Originally Posted by d_stranz View Post
    If you want to force a given row x cell count, then you should probably add QSpacerItem instances to at least one of each of the left / right and top / bottom edge cells as placeholders to force the rest of the cells to the upper left.
    Or even have an additional fourth column that contains vertical spacers which ensure each row has the desired height and a third row with horizontal spacers to ensure each column has the desired width.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set fixed row and column number in QGridLayout

    Yeah I think adding an item will be the way now. Surprised Qt doesn't have a better way to do it.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set fixed row and column number in QGridLayout

    Surprised Qt doesn't have a better way to do it.
    Like I said, Qt layouts are designed to adapt dynamically to their content. You're asking grid layout to do something unnatural - adopt a fixed layout regardless of content. You can either develop your own "fixed grid layout" or you can (through code or Qt Designer) add spacers to force the behavior you want. That's about as good as it gets.

  7. #7
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set fixed row and column number in QGridLayout

    Quote Originally Posted by d_stranz View Post
    Like I said, Qt layouts are designed to adapt dynamically to their content. You're asking grid layout to do something unnatural - adopt a fixed layout regardless of content. You can either develop your own "fixed grid layout" or you can (through code or Qt Designer) add spacers to force the behavior you want. That's about as good as it gets.
    I don't think the fixed row/column layout is unnatural. On my Android phone I set the home screen to be 4 rows and 4 columns on each page. Regardless of how my apps I placed on one page, they must located on one of the 16 positions. I think this should be common for most of the desktop/home screen, isn't it?

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set fixed row and column number in QGridLayout

    It is unnatural for a QLayout. Of course there are other GUIs in use where things are placed on a fixed grid, like your phone, Excel spreadsheets, or a calendar. Geez, don't take things so literally.

Similar Threads

  1. Replies: 6
    Last Post: 30th December 2012, 22:10
  2. Replies: 1
    Last Post: 5th March 2012, 06:34
  3. Replies: 2
    Last Post: 5th February 2009, 11:21
  4. Fixed Column in QTreeview
    By ormonde in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 07:49
  5. Limit number of row/column in QGridLayout
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2007, 19:54

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.