Results 1 to 3 of 3

Thread: Issues with resizing QTableView nested in another widget

  1. #1
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Issues with resizing QTableView nested in another widget

    I'm having a few issues with sizing my QTableView subclass which is nested inside a few other widgets. What I'm trying to achieve is a TableView that increases it's vertical size to accommodate all the rows in the table. I've been able to get close to this, but not quite there. I have a suspicion that I am approaching some aspects of the problem the wrong way.

    The basic layout of my Application is this:

    MainWindow contains a TabWidget the Tabs have a ScrollArea which contains several collapsible panels in a vertical layout that serve as containers for various input widgets. Some of those panels contain my TableView class. In order to get the correct sizing for my TableVIew, I implemented SizeHint, and defined it as an override because otherwise it wouldn't be called at all. I'm fairly certain this is because Tables typically use SizeHintForIndex, SizeHintForColumn, and SizeHintForRow, but I really only need to base my sizing on the table itself, so hopefully this is an acceptable solution.

    Qt Code:
    1. QSize PTableView::sizeHint() const{
    2. QSize size = QTableView::sizeHint();
    3. size.setHeight(defaultCellHeight * model()->rowCount() + 45);
    4. return size;
    5. }
    To copy to clipboard, switch view to plain text mode 

    The good news is this works perfectly when the widget is constructed. SizeHint is called appropriately, and the TableView is the correct vertical size (horizontal size isn't an issue as it simply takes up all available space). So my issue is that when the user adds a new row, or deletes a row, the size does not update. So I tried to make this connection to solve the problem:

    Qt Code:
    1. connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(recalculateSize()));
    2. connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(recalculateSize()));
    3.  
    4. void PTableView::recalculateSize(){
    5. adjustSize();
    6. }
    To copy to clipboard, switch view to plain text mode 

    This produced undesirable results. The vertial size is correct for the TableView, but the container widget does not resize to match. Additionally, when the window is resized the widget reverts back to its constructed size.

    So my question I suppose boils down to: how can I tell my widget to resize in the same way that it does when the tab is constructed? If I add a bunch of rows, close the project, then reopen it, the Table will have been resized to the correct size, so that is the sizing I need.

    If need be, I can post my full classes or a compilable example, but I think I just need a push in the right direction to figure it out.

  2. #2
    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: Issues with resizing QTableView nested in another widget

    You might have to call activate() on the view's parent's layout.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issues with resizing QTableView nested in another widget

    Quote Originally Posted by anda_skoa View Post
    You might have to call activate() on the view's parent's layout.

    Cheers,
    _
    I tried calling activate at various levels of my widget hierarchy, and none seemed to produce any noticeable effect. The oddest thing to me is that the size is calculated correctly when the widgets within the tab are all constructed. I just can't seem to determine how I can make that happen again. I tried looking at the call stack when sizeHint is called during construction, but I didn't really get much info from that except that the GridLayout that the view is in seems to be asking for it's size.


    Added after 6 minutes:


    Actually I might have a workable solution now. Instead of calling adjust size, I am simply setting a fixed height based on my sizeHint. It's not the best solution, but I don't think it will cause any issues. I do wish I understood the sizing and layout system better though.
    Last edited by forgottenduck; 27th March 2015 at 21:08.

Similar Threads

  1. Nested QDockWidget resize issues
    By jdisrael in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2014, 23:45
  2. nested QTableView objects
    By Schluchti in forum Qt Programming
    Replies: 0
    Last Post: 8th February 2014, 15:06
  3. Resizing Issues
    By Jtjr008 in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2012, 16:30
  4. Widget resizing issues
    By SKolaMunn in forum Newbie
    Replies: 0
    Last Post: 15th September 2011, 16:27
  5. Issues in nested QDialog
    By Askar in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2011, 13:59

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.