PDA

View Full Version : resize to content of a QTreeWidget



torres
29th March 2013, 16:21
I have a widget with a QLineEdit and a QTreeWidget in a QVBoxLayout. I would like to size MyWidget to adjust to the longest string in the treeWidget.
Do I need to override sizeHint() or can qt do it somehow? When I type in the QLineEdit, it automatically adjusts the items in QTreeWidget.



MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);

QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
vBoxLayout->setContentsMargins(1, 1, 0, 0);
mLineEdit = new QLineEdit;

vBoxLayout->addWidget(mLineEdit);

mTreeWidget = new MyTreeWidget;
mTreeWidget->setWindowFlags(Qt::Popup);
mTreeWidget->setFocusPolicy(Qt::NoFocus);
mTreeWidget->setMouseTracking(true);
mTreeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

mTreeWidget->setColumnCount(1);
mTreeWidget->setUniformRowHeights(true);
mTreeWidget->setRootIsDecorated(false);
mTreeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
mTreeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
mTreeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
mTreeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
mTreeWidget->header()->hide();
mTreeWidget->resizeColumnToContents(0);

mLineEdit->installEventFilter(this);
vBoxLayout->addWidget(mTreeWidget);
connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
connect(mTreeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
move(QCursor::pos());
show();
mLineEdit->setFocus();
setLayout(vBoxLayout);
layout()->setSizeConstraint(QLayout::SetFixedSize);
}

torres
2nd April 2013, 17:54
Maybe I wasn't clear: the second item has the longest string and I would expect the QTreeWidget to grow to show the whole string.
I added an example with the content of the QTreeWidget.
Any ideas why it doesn't resize to the longest string?




#include <QVBoxLayout>
#include <QLineEdit>
#include <QTreeWidget>
#include <QLineEdit>
#include <QTreeWidget>

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);

QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
vBoxLayout->setContentsMargins(1, 1, 0, 0);
QLineEdit *mLineEdit = new QLineEdit;

vBoxLayout->addWidget(mLineEdit);

QTreeWidget *treeWidget = new QTreeWidget;
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, "The quick brown fox jumps over the lazy dog");
treeWidget->addTopLevelItem(item);
QTreeWidgetItem *item2 = new QTreeWidgetItem();
item2->setText(0, "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog");
treeWidget->addTopLevelItem(item2);
QTreeWidgetItem *item3 = new QTreeWidgetItem();
item3->setText(0, "The quick brown");
treeWidget->addTopLevelItem(item3);


treeWidget->setWindowFlags(Qt::Popup);
treeWidget->setFocusPolicy(Qt::NoFocus);
treeWidget->setMouseTracking(true);
treeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

treeWidget->setColumnCount(1);
treeWidget->setUniformRowHeights(true);
treeWidget->setRootIsDecorated(false);
treeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
treeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
treeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
treeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
treeWidget->header()->hide();
treeWidget->resizeColumnToContents(0);

mLineEdit->installEventFilter(this);
vBoxLayout->addWidget(treeWidget);
connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
connect(treeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
move(QCursor::pos());
show();
mLineEdit->setFocus();
setLayout(vBoxLayout);
layout()->setSizeConstraint(QLayout::SetFixedSize);
}

torres
8th April 2013, 23:13
Does nobody have an idea? Please let me know if I'm not clear or if you need more code sample.

Thanks!

torres
23rd April 2013, 02:34
Any ideas?

ChrisW67
23rd April 2013, 05:14
There is no natural size for a tree view. A QTreeView is a QAbstractScrollArea. If the content, i.e. the table cells, grow larger than the area available to display it, dictated by the containing layout, then the scroll area gains scroll bars. If you force the horizontal scroll bars to stay hidden then you will have no way to access data outside the current view port. Under no circumstance will the table view automatically resize itself; its sizeHint() is not derived from its content.

If you want a column to automatically occupy all the remaining space within the current view port then you want to set the section's resize mode QHeaderView::setResizeMode() to QHeaderView::Stretch. If you want the column to always match the content within it then set the resize mode to QHeaderView::ResizeToContents. The QTreeView::setWordWrap() function may also be of use to make longer items viewable. None of these options changes the size of the QTreeView.

If you really want to resize tree view based on its content then you will need to code to calculate the "correct" size and force it. Set all the sections to QHeaderView::ResizeToContents and query them for their size after every data update to get a reasonable starting point. No matter how much sense it makes you eventually hit the point where the content is too wide for the screen and you are back to where you started.