Results 1 to 8 of 8

Thread: Resizing QTableView to eliminate horizontal scroll bar?

  1. #1
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Resizing QTableView to eliminate horizontal scroll bar?

    Hi folks,

    I'm adding a QTableView into a QVBoxLayout on top of a QPushButton, as follows:

    Qt Code:
    1. QVBoxLayout* mainLayout = new QVBoxLayout;
    2.  
    3. QPushButton* btn_OK = new QPushButton(tr("OK"));
    4. connect(btn_OK, SIGNAL(clicked()), this, SLOT(accept()));
    5.  
    6. model = NULL;
    7. loadModel ();
    8.  
    9. tableView = new QTableView(this);
    10. tableView->setModel (model);
    11. tableView->verticalHeader()->hide();
    12. tableView->horizontalHeader()->setStretchLastSection (true);
    13. tableView->setSelectionMode (QAbstractItemView::SingleSelection);
    14. tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    15.  
    16. mainLayout->addWidget(tableView);
    17. mainLayout->addWidget(btn_OK);
    18.  
    19. setLayout (mainLayout);
    To copy to clipboard, switch view to plain text mode 

    This displays everything, but the dialog's initial size is too narrow and the QTableView uses a horizontal scroll bar. I would like to:
    -initially size the dialog so that all the columns are shown without the need to scroll
    -prevent the user from resizing the dialog smaller than this size

    I have tried setting size policies on the QTableView and the Dialog, turning off the scroll bar, etc. but am not having any luck. Do I need to reimplement sizeHint() for the dialog? Any ideas would be really appreciated.

    Russ

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    Have you tried QWidget::adjustSize()?

  3. #3
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    I did try adjustSize, but potentially on the wrong "widget". Which should I use it on? Do I need to set any size policies beforehand?

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    As Lykurg told you you can adjust the size of your widget through QWidget::adjustSize()
    or compute the minimum width necessary not to show the horizontal scroll bar, using QWidget::minimumSizeHint () (QTableView inherits QWidget):
    Qt Code:
    1. MyTableView::minimumSizeHint()
    2. (
    3. compute the minimum width of your tableView in this function
    4. QSize widgetWidth = theRigthWidth;
    5. return widgetWidth;
    6. )
    To copy to clipboard, switch view to plain text mode 

    and implement this minimum value in MyTableView::sizeHint()

    Qt Code:
    1. MyTableView::sizeHint()
    2. {
    3. return minimumSizeHint();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by toutarrive; 17th March 2010 at 21:14.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    Try to only add adjustSize(); after setLayout() of after you "exec()" your dialog.

  6. #6
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    Lykurg - Have tried that, but still getting the same result. It's as though QTableView returns a sizeHint() that allows for scroll bars in the view.

    toutarrive - Is there a simple way you know of to calculate the minimum required size? It seems silly to add up each column width and try to account for gridlines, margins, etc. There has to be a simpler way that doesn't involve subclassing.

    The example code for the item views sets an arbitrary size for the window instead of allowing the layout manager to do the work. Perhaps this is why?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    To calculate a custom size hint for your table view you could iterate over the columns using QTableView::columnWidth(). Or you can try if you get the width with QTableView::horizontalHeader() + QFrame::frameWidth()/QWidget::width().

  8. #8
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing QTableView to eliminate horizontal scroll bar?

    If you don't want to subclass QTableView, use QTableView::setMinimumSize ( const QSize & )
    or QTableView::setminimumWidth(int).

    As for the minimum size , you will still have to do some calculations in order not to display the horizontal scrollbar.
    As we don't now that much about what goes in your TableView, to start off with you could try to implement it as Lykurg advices you.
    Last edited by toutarrive; 18th March 2010 at 08:17.

Similar Threads

  1. Grid QTableView horizontal only
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 16:14
  2. QTableView Horizontal Bar depressed, looks ugly.
    By killerwookie99 in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2009, 14:01
  3. Vertical scroll bar, but resizable horizontal content
    By minimoog in forum Qt Programming
    Replies: 0
    Last Post: 14th January 2009, 20:51
  4. QTableView Horizontal Header's Width
    By araglin in forum Newbie
    Replies: 3
    Last Post: 21st December 2008, 08:54
  5. ListWidget horizontal scroll.
    By patrick772goh in forum Qt Tools
    Replies: 3
    Last Post: 17th July 2007, 07:32

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.