Results 1 to 4 of 4

Thread: QTableView sizeHint() issues

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTableView sizeHint() issues

    What if you return the same size in minimumSizeHint() instead? Oh, and you might also have to add frames to that calculation.
    J-P Nurmi

  2. The following user says thank you to jpn for this useful post:

    yuriry (21st September 2008)

  3. #2
    Join Date
    Jun 2008
    Posts
    49
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sizeHint() issues

    Quote Originally Posted by jpn View Post
    What if you return the same size in minimumSizeHint() instead? Oh, and you might also have to add frames to that calculation.
    it seems minimumSizeHint() has the same result

    as for frames - how do I get frame sizes to add to the calculation?

    also, my table view doesn't have anything but its contents really - the real size of the widget is nothing but the sizes of the cells added together. if I manually resize the window, this is what I get as well (e.g. the widget is really 480x480 for the above example of 12x12 pixel cells and 40x40 cells overall)

  4. #3
    Join Date
    Sep 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 10 Times in 9 Posts

    Default Re: QTableView sizeHint() issues

    I also needed to layout table views without displaying their scrollbars. sizeHint() and minimumSizeHint() did the work for me. Below is the code of a sub-class of the QTableView:

    Qt Code:
    1. MyTableView::MyTableView(QWidget* parent)
    2. : QTableView(parent)
    3. {
    4. }
    5.  
    6. QSize MyTableView::sizeHint() const
    7. {
    8. return minimumSizeHint();
    9. }
    10.  
    11. QSize MyTableView::minimumSizeHint() const
    12. {
    13. QAbstractItemModel* m = model();
    14.  
    15. int colCount = m->columnCount();
    16. int w = 0;
    17. for(int i=0; i<colCount; ++i) {
    18. w += columnWidth(i);
    19. }
    20.  
    21. int rowCount = m->rowCount();
    22. int h = 0;
    23. for(int i=0; i<rowCount; ++i) {
    24. h += rowHeight(i);
    25. }
    26.  
    27. int doubleFrame = 2 * frameWidth();
    28.  
    29. w += verticalHeader()->width() + doubleFrame;
    30. h += horizontalHeader()->height() + doubleFrame;
    31.  
    32. return QSize(w, h);
    33. }
    To copy to clipboard, switch view to plain text mode 

    Here is alternative code for minimumSizeHint() which also does the work:

    Qt Code:
    1. QSize MyTableView::minimumSizeHint() const
    2. {
    3. QHeaderView* vHeader = verticalHeader();
    4. QHeaderView* hHeader = horizontalHeader();
    5.  
    6. int doubleFrame = 2 * frameWidth();
    7.  
    8. int w = hHeader->length() + vHeader->width() + doubleFrame;
    9. int h = vHeader->length() + hHeader->height() + doubleFrame;
    10.  
    11. return QSize(w, h);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I like this second implementation better because it is shorter but I'm not sure if it'll work in all cases. I'm actually not sure if the first implementation will also work in all cases, so if someone with more Qt experience could give an advice I would really appreciate it.

    Thank you in advance,
    Yuri

  5. The following user says thank you to yuriry for this useful post:

    mattc (11th October 2009)

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
  •  
Qt is a trademark of The Qt Company.