Results 1 to 4 of 4

Thread: QTableView sizeHint() issues

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. 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.