PDA

View Full Version : Autoresize widget, containing QTreeView, when the second is expanded



Cucumber
30th October 2010, 18:14
Hi there,
What I would like to know is how can I change widget's width when QTreeView, that inside of that widget expands?
In other words, I have a QTreeView and I would like to resize the width of columns with the resizeColumnToContents() method if anything expands therefore QTreeView may not fit in the main widget.
How can I do that? I've tried connecting QTreeView::expand(QModelIndex&) with layout's activate() method, but had no luck as I don't know how to use the QModelIndex parameter. Is there any other options? Inside of QTreeView I have a subclassed QAbstractItemModel.
Thanks in advance!

wysota
30th October 2010, 18:57
Could you explain what you mean by "QTreeView may not fit in the main widget"?


How can I do that? I've tried connecting QTreeView::expand(QModelIndex&) with layout's activate() method, but had no luck as I don't know how to use the QModelIndex parameter.
I'd suggest reading what a particular method does instead of using it blindly.

Cucumber
31st October 2010, 21:15
Could you explain what you mean by "QTreeView may not fit in the main widget"?

Yeah, by that I've meant that if, for example:
QTreeView's width is 400 px and the MainWindow's 400 px + epsilon,
but, after expanding some QTreeView item, it's width become larger, for example, 500 px.
The main widget in that situation will still have the same width and will display sliders.
How to autoresize it?
Can it be done by connecting some QTreeView's, MainWindow's, or even QAbstractItemModel's methods?
Or should I re-implement some displaying methods?

Cucumber
1st November 2010, 15:21
To make things more clear, I provide you with couple screenshots:

http://cl.ly/4c332b80d9c0075146ea/content

If I expand the last item, the contents wouldn't fit the window. I would like to resize it that way:

http://cl.ly/365cef56a16ceeb16afe/content

What is the best way to do it?
Thanks in advance!

wysota
2nd November 2010, 18:20
How to autoresize it?
Reimplement the widget's sizeHint() method.

Cucumber
3rd November 2010, 14:51
Reimplement the widget's sizeHint() method.
Thanks! I re-implement it with following:

QSize TreeView::sizeHint() const
{
int doubleFrame = 2 * frameWidth();

int w = header()->length() + doubleFrame;
int h = height() + doubleFrame;

return QSize(w, h);
}

As well, I connect(treeView, SIGNAL(expanded(const QModelIndex&)), treeView, SLOT(needResize(const QModelIndex&)));


void TreeView::needResize(const QModelIndex& index)
{
resize(sizeHint());
updateGeometry();
}
And it works. Have I done it right?

wysota
3rd November 2010, 15:21
No. If you need the needResize() slot then it means your widget is not in a layout. You only need to call updateGeometry() but not resize(sizeHint()).