Results 1 to 6 of 6

Thread: QGridLayout: insert row in middle

  1. #1
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default QGridLayout: insert row in middle

    Hi,

    I have a QGridLayout and after some action I would like to insert new rows at some specified row. Can I do this without manually moving all the widgets below?

    Thanks,

    Thomas

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGridLayout: insert row in middle

    you can use
    Qt Code:
    1. void QGridLayout::addWidget ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 );
    2.  
    3. specify the "fromRow", all the widgets will be automatically adjusted.
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGridLayout: insert row in middle

    to clarify (example)

    I have a QGridLayout with already 20 rows full of widgets. Now I would like to insert a new row between row 9 and row 10. I think that this can not be done with the addWidget function?

    Thanks,

    Thomas

  4. #4
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGridLayout: insert row in middle

    I have the same question as the OP. Using the code in Santosh's response will replace whatever widgets are already in fromRow, fromColumn, not insert a new row/column. This is easy to do in Qt Designer, but I can't tell if there's a way to do it programmatically without manually moving every widget already in the QGridLayout.

  5. #5
    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: QGridLayout: insert row in middle

    There's no function to do it that I can see. For a simple grid of unspanned widgets it's trivial to move everything down a row to "open" a new row (or remove a row). Spanned widgets make it more difficult. However, if you need to do this a lot (for identical rows) then consider using a QVBoxLayout containing nested QHBoxLayouts for each row. You then have access to QBoxLayout::insertLayout() to insert new rows and QBoxLayout::takeAt() to remove them. Alternatively a QTableView/QTableWidget may be a viable option.

  6. The following user says thank you to ChrisW67 for this useful post:

    Phlucious (31st May 2012)

  7. #6
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGridLayout: insert row in middle

    Good advice, thanks. Moving widgets manually didn't seem quite so trivial so I appreciate your suggestion of the QVBoxLayout. I thought at first that I had to use a QGridLayout to ensure that each row's components lined up as columns, but it just occurred to me that I can accomplish the same thing using widget size controls. I'll probably end up going that route.

    Using QTableView would be the ideal, imo, but one of the columns has three different editable components and I couldn't figure out how to have a delegate create an editor for all three if they were in the same "cell." The model/view architecture has a very high learning curve when first getting started.

Similar Threads

  1. Drag/Drop on middle mouse button?
    By joeld42 in forum Qt Programming
    Replies: 0
    Last Post: 21st August 2010, 00:20
  2. Set size when insert a QWidget into a QGridLayout
    By Tondog in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 06:35
  3. QStyle : how to make the QMenu display in middle?
    By nish in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 27th November 2008, 10:39
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01
  5. Middle Button Mouse
    By jaime in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2006, 03:01

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.