PDA

View Full Version : Resizing QTableView to eliminate horizontal scroll bar?



russford
17th March 2010, 20:28
Hi folks,

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



QVBoxLayout* mainLayout = new QVBoxLayout;

QPushButton* btn_OK = new QPushButton(tr("OK"));
connect(btn_OK, SIGNAL(clicked()), this, SLOT(accept()));

model = NULL;
loadModel ();

tableView = new QTableView(this);
tableView->setModel (model);
tableView->verticalHeader()->hide();
tableView->horizontalHeader()->setStretchLastSection (true);
tableView->setSelectionMode (QAbstractItemView::SingleSelection);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows );

mainLayout->addWidget(tableView);
mainLayout->addWidget(btn_OK);

setLayout (mainLayout);


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

Lykurg
17th March 2010, 20:37
Have you tried QWidget::adjustSize()?

russford
17th March 2010, 21:30
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?

toutarrive
17th March 2010, 21:48
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):


MyTableView::minimumSizeHint()
(
compute the minimum width of your tableView in this function
QSize widgetWidth = theRigthWidth;
return widgetWidth;
)


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



MyTableView::sizeHint()
{
return minimumSizeHint();
}

Lykurg
17th March 2010, 22:10
Try to only add adjustSize(); after setLayout() of after you "exec()" your dialog.

russford
18th March 2010, 03:18
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?

Lykurg
18th March 2010, 08:07
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().

toutarrive
18th March 2010, 08:21
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.