PDA

View Full Version : resizing a QTreeWidget



drhex
30th September 2006, 23:53
I'm trying to get a QTreeWidget to show a text column and some equally sized checkbox columns. This eventually works, but the QTreeWidget seems hell-bent on showing up at 256x192 pixels, although the stuff put in it requires less space.

QWidget::adjustSize() claims to resize a widget to fit the contents, but does not.
Calling resize() on the QTreeWidget helps if it is a top level widget, but does nothing if it is in a layout together with other widgets.

It is possible to let the user manually adjust the size after it is rendered, but how does one resize a QTreeWidget to fit its contents from code? (Tested in Qt-4.2.0-rc1)

/Joakim Rosqvist



#include <QApplication>
#include <QTreeWidget>
#include <QHeaderView>
#include <QHBoxLayout>

int main( int argc, char **argv )
{
QApplication app( argc, argv );

QWidget *hbox = new QWidget;
QHBoxLayout *lay;
hbox->setLayout(lay = new QHBoxLayout);

QTreeWidget *tw = new QTreeWidget;
tw->setColumnCount(3);
QTreeWidgetItem *twi = new QTreeWidgetItem;
twi->setText(0,"Text");
twi->setCheckState(1,Qt::Checked);
twi->setCheckState(2,Qt::Checked);
tw->addTopLevelItem(twi);
tw->resizeColumnToContents(0);
tw->resizeColumnToContents(1);
tw->resizeColumnToContents(2);
tw->header()->setResizeMode(0, QHeaderView::Interactive);
tw->header()->setResizeMode(1, QHeaderView::ResizeToContents);
tw->header()->setResizeMode(2, QHeaderView::ResizeToContents);
tw->header()->setStretchLastSection(false);

tw->adjustSize(); // no effect!
tw->resize(110,100); // no effect!

lay->addWidget(tw);
hbox->show();
app.exec();
return 0;
}

wysota
1st October 2006, 00:00
You have to set sizeHint and/or sizePolicy. It is possible that setMinimumSize() along with size policy set to "Minimum" is enough to achieve what you need.

drhex
1st October 2006, 12:10
I tried playing around with sizePolicy and minimumSize, but that did not accomplish anything useful.

Reimplementing sizeHint works better, e.g.:



QSize MyTreeWidget::sizeHint() const
{
int width=0;
for (int i=0; i<columnCount(); ++i)
width += 2 + columnWidth(i);
return QSize(width +16-2, 100);
}


That is, I basically add up the column-widths. Unfortunately I had to hardcode in "2" for the spacing between columns that is not included in the columnWidth(), and a "16" for the width of a scrollbar. Is there any way to ask Qt about these numbers?

wysota
1st October 2006, 12:28
Is there any way to ask Qt about these numbers?


"2" for the spacing between columns
Probably not.


"16" for the width of a scrollbar

verticalScrollbar()->width() should do the trick. Just check if the scrollbar should be visible at all.

drhex
1st October 2006, 18:04
Just check if the scrollbar should be visible at all.

...by knowing how many item one wants to show and how much space each
of them will use up and how much space there is available, I presume.

Qt doesn't seem to know [whether a scrollbar is needed] by the time sizeHint is called;
The scrollbar width() always returns 100 there, it changes into 16 once it is actually rendered on screen.

wysota
1st October 2006, 18:20
...by knowing how many item one wants to show and how much space each
of them will use up and how much space there is available, I presume.
I don't know is such assumptions are appropriate. Calling verticalScrollBar()->isVisible() might be more reliable.


Qt doesn't seem to know [whether a scrollbar is needed] by the time sizeHint is called;
That doesn't matter. You can change the size hint once the widget is rendered for the first time using QWidget::updateGeometry().

The scrollbar width() always returns 100 there, it changes into 16 once it is actually rendered on screen.
Yes, that's normal for every widget.

drhex
27th October 2006, 23:32
Just found a way to get the size of the scrollbar without having to display one first :)


qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent)