Results 1 to 8 of 8

Thread: QTableWidget cells are QListWidgets resizeRowToContents

  1. #1
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default QTableWidget cells are QListWidgets resizeRowToContents

    I have a QTableWidget inside a QVBoxLayout. Its cells are comprised of QListWidgets.
    How can I resize the rows of the table to take in account the number of QListWidgetItems.
    The table header and cells expand and contract fine, but when I go to full screen, I'd like the row to expand to show the maximum number of list items for that row.
    Also, if a row has fewer items, the row should be shorter.

    Qt Code:
    1. listwidget->setResizeMode(QListView::Adjust); // has no effect
    2. tableWidget->resizeRowToContents(row); // has no effect
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget cells are QListWidgets resizeRowToContents

    Try "tableWidget->verticalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );"
    I also use "horizontalHeader()->setStretchLastSection( true );" but maybe not needed here.
    Maybe you need to reimplement sizeHint / minimumSizeHint too.

  3. #3
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget cells are QListWidgets resizeRowToContents

    QHeaderView::ResizeToContents breaks the expanding layout, i.e. the cells no longer expand/contract when resizing the window.
    The following works as far as resizing goes, but all the cells are the same size. If a row has only 2 lines max (2 listWidgetItems), I want that entire row to resizeRowToContents() and display 2 lines.
    Qt Code:
    1. QHeaderView* columnheader = ui->tableWidget->horizontalHeader();
    2. columnheader->setSectionResizeMode(QHeaderView::Stretch);
    3.  
    4. QHeaderView* rowheader = ui->tableWidget->verticalHeader();
    5. rowheader->setSectionResizeMode(QHeaderView::Stretch);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: QTableWidget cells are QListWidgets resizeRowToContents

    You don't mention what you are using as a model, but I would suggest that you do this.

    Derive from QAbstractItemModel. Implement Qt::SizeHintRole case for QAbstractItemModel::data(). In this, compute the dimensions of each list based on the number of items in it, font, etc. and return that as a QSize. I believe the table view will use this to set the row and column dimensions by choosing the maximum for each respective row and column.

    It sounds like your model is dedicated to viewing in a single table view. If you want the cell sizes to change based on whether the table view is full screen or not, then setting a flag in the model to indicate the view state would let you switch between a compressed or fully-expanded list size in the data() method.

    I also presume you are using QAbstractItemView::setIndexWidget() when you create the list widgets for each cell. You might also need to store this pointer in the model using QAbstractItemModel::setData() (maybe under Qt::UserRole) so you can retrieve it when calculating the size hint.

    Edit: I think what is happening now is that the cells might be sized according to the list widgets' minimumSizeHint(), so trying to change things by manipulating table properties isn't going to work. You might be able to verify this by setting a minimumSizeHint() for each list widget and see if the layout changes.
    Last edited by d_stranz; 28th January 2015 at 18:37.

  5. #5
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget cells are QListWidgets resizeRowToContents

    You might be able to verify this by setting a minimumSizeHint() for each list widget and see if the layout changes.
    I can't find anything like setMinimumSizeHint().

  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: QTableWidget cells are QListWidgets resizeRowToContents

    My bad. If you implement a custom widget, you can override minimumSizeHint() to return something other than the default (which for QWidget is an invalid size). Try setting a minimumSize instead - something much larger than the size your table cells are now. A minimumSize that is larger than the default will take precedence over the minimumSizeHint.

  7. #7
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableWidget cells are QListWidgets resizeRowToContents

    Setting a larger minimumSize just hoses the table grid, perhaps the table row size, yet the listwidgets remain the same size.
    The one thing that affects the row size is numbers of rows.
    If I break the centralWidget vertical layout, things like
    Qt Code:
    1. listWidget->setMaximumHeight(100);
    2. tableWidget->setCellWidget(1, 1, listWidget);
    3. tableWidget->resizeRowToContents(1);
    To copy to clipboard, switch view to plain text mode 
    work great.
    The culprit is the layout.
    Last edited by Henry Blue Heeler; 29th January 2015 at 06:41.

  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: QTableWidget cells are QListWidgets resizeRowToContents

    The one thing that affects the row size is numbers of rows.
    You must be doing something funky. I don't recall ever seeing a table view's row size change when the number of rows changes. If there are fewer rows than the space available, then the space below the bottom row is just empty. The rows don't expand to fit the size.

Similar Threads

  1. QTableWidget exclusive cells
    By bwnicewo in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2012, 01:25
  2. [How to ?] Writing QTableWidget cells.
    By Rewo in forum Newbie
    Replies: 6
    Last Post: 4th April 2010, 12:00
  3. read QTableWidget cells
    By navid in forum Newbie
    Replies: 8
    Last Post: 4th April 2010, 11:40
  4. Merging cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 1
    Last Post: 22nd January 2010, 20:15
  5. Focus of cells in a QTableWidget
    By SailinShoes in forum Qt Programming
    Replies: 4
    Last Post: 9th June 2008, 09:19

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.