Results 1 to 11 of 11

Thread: Custom QTableView width

  1. #1
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom QTableView width

    Hey Everyone,

    I have made a QTableView subclass which I wanted to resize to fit the columns within it. Basically, I cannot for the life of me figure out what I am missing here. There are an extra 11 pixels that I need to add from some header or border or something, which remains independent of the number of columns in the QTableView. There is no vertical header

    I can, live the having the 11 pixels hard coded in here if I have to, but does anyone know what these pixels are accounting for?

    Qt Code:
    1. int TableViewCopy::maximumWidthHint()
    2. {
    3. int colWidth = 0;
    4. int count = model()->columnCount();
    5.  
    6. for(int i = 0; i < count; i++)
    7. if(!isColumnHidden(i))
    8. colWidth += columnWidth(i);
    9.  
    10. int tableWidth =
    11. (colWidth + // total column width
    12. verticalScrollBar()->height() + // Need room for the vertical scrollbar
    13. 11); // Why do I need 11 pixels to make it view correctly? (Windows7x64 Aero)
    14.  
    15. return tableWidth;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for your help everyone!

    - Michael

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom QTableView width

    Use horizontal header view to calculate the column width, then no need to account for scroll bar, use this way

    Qt Code:
    1. int TableViewCopy::maximumWidthHint()
    2. {
    3. //QHeaderView * view = header(); // use this for QTreeView
    4. QHeaderView * view = horizontalHeader(); // use this for QTableView
    5.  
    6. int sections = view->count();
    7. int tableWidth = 0;
    8.  
    9. for(int i = 0; i < sections; i++)
    10. tableWidth += view->sectionSize(i);
    11.  
    12. return tableWidth;
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QTableView width

    Thanks Santosh Reddy,

    Implementing in this way appears to be the same as accessing the length property, as below.

    Qt Code:
    1. int TableViewCopy::maximumWidthHint()
    2. {
    3. //QHeaderView * view = header(); // use this for QTreeView
    4. QHeaderView * view = horizontalHeader(); // use this for QTableView
    5.  
    6. return view->length();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Either way this is not producing the desired result. The size of the QTableView is still smaller than the content by a different number of pixels (maybe 30ish). Is it likely that elsewhere my code is effecting the size that this QTableView should take? Or am I missing something simple here?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom QTableView width

    I am able set the TreeView's width using this method, the width exatly fits the treeview, (I expect it to be same with tableview), may be there is somthing else in you code, which is causing this.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,323
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom QTableView width

    Does the column width include the pixels used for the grid and/or resize handles between columns? Does it include any padding between columns? Does it include the viewport margins? Frame width? Each of the base classes for QTableView applies some formatting to the view, so that could account for the missing pixels.

  6. #6
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QTableView width

    Santosh: Hmm, that is strange because I don't think I have really changed anything that would effect the QTableView's width.

    Here is an example of how I have implemented it one of my tables.

    Qt Code:
    1. TableViewCopy *chanDataTable = new TableViewCopy;
    2. chanDataTable->setModel(channelData->model);
    3. chanDataTable->setShowGrid(false);
    4. chanDataTable->setEditTriggers(QAbstractItemView::NoEditTriggers); // table can not be editted
    5. chanDataTable->resizeColumnsToContents();
    6. chanDataTable->setSortingEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    d_stranz: I've tried to find a relationship between the column count and the excess number of pixels, but it seems not to be existent. I had a bit of a look at ViewPorts but will probably need to investigate this further.

    Thanks for the replies,
    Michael

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom QTableView width

    looks like there is no simple straight forward solution for this, I am trying to create a custom QTreeView to find a solution for this, will post updates soon

  8. The following user says thank you to Santosh Reddy for this useful post:

    Cotlone (14th July 2011)

  9. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom QTableView width

    check this out
    Qt Code:
    1. int TableViewCopy::maximumWidthHint()
    2. {
    3. // Add 2 Pixels to conpensate for the view port's border rectangle
    4. // if custom view port is used then change this value accordingly
    5. return horizontalHeader()->length() + verticalHeader()->width() + 2;
    6. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QTableView width

    Hmm, this doesn't seem to work for me either. It is strange indeed.
    I'll keep looking into it when I get a little more time on my hands.

  11. #10
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom QTableView width

    Oh I had this problem!
    This 11px comes from default layout margin, for me 'table->x()' gives this 11px and I change it by 'QLayout::setContentsMargins(int,int,int,int)'
    edit: a typo in function name!

  12. #11
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QTableView width

    Hi everyone, thanks for the help.

    Sorry about the latency of this response, it been pretty busy here.

    Anyway, I had a look for the table->x() value and it wasn't what I was after. For me, it has a value of zero.

    My search continues...

    Cheers,
    Michael

Similar Threads

  1. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  2. Column width sensitive kerning in QTableView
    By Debilski in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2009, 19:11
  3. QTableView Horizontal Header's Width
    By araglin in forum Newbie
    Replies: 3
    Last Post: 21st December 2008, 08:54
  4. How to set QTableView width to width of horizontal header?
    By martinb0820 in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 20:51
  5. set the width of the columns of QTableView
    By zhanglr in forum Qt Programming
    Replies: 6
    Last Post: 31st July 2008, 15:29

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.