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 )
{
tw->setColumnCount(3);
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks