Results 1 to 4 of 4

Thread: QTableView sizeHint() issues

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

    Question QTableView sizeHint() issues

    I'm working off the itemviews/pixelator example that is provided by Qt, and I'm having trouble making the subclassed QTableView appear in it's proper size. My impression was that changing line 144 of mainwindow.cpp to adjustSize(); would make everything fit right, but it doesn't. This is despite the delegate returns a 12x12 pixel size for its sizeHint(), and the model returns proper column and row counts.

    I searched on the issue, and saw that one would need to implement the sizeHint() method for the subclasses QTableView object. Fair enough, I did so. While the situation is better now, for some reason the QTableView widget is shown smaller that it should be, and is thus surrounded by scrollbars still. for example when the delegate returns 12x12 for sizeHint(), and the model reports 40 rows and 40 columns, the overloaded sizeHint() returns 480x480 pixels appropriately - but the widget itself is actually crancked into what seems to be about 480x455 (including the scrollbars), or 460x440 visible inside the scrollbars.

    what is going wrong here? what else should be done, besides overriding sizeHint()?

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

    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

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

    yuriry (21st September 2008)

  4. #3
    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)

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

    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

  6. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.