[Interest] QTreeView, QHeaderview, ResizeToContents and Interactive

Etienne Sandré-Chardonnal etienne.sandre at m4x.org
Fri Mar 17 09:22:21 CET 2017


When Qt manages to do something with a built in function, but you can't do
it manually, the best way is to look into Qt code to understand how it's
done internally. Qt source is a very good way to find answers...

Into qheaderview.cpp, you may find that ResizeToContents is used inside
resizeSections(). That last function calls viewSectionSizeHint, which in
turn calls the view->sizeHintForColumn()

Cheers,

Etienne

2017-03-17 9:18 GMT+01:00 Etienne Sandré-Chardonnal <etienne.sandre at m4x.org>
:

> Sorry, I wrote too fast
>
> The proper way to get the column width ist tableView->sizeHintForColumn(i)
>
> So just do
>
> for(int i=0;i<header->count();++i)
>     header->resizeSection(i, qMax(header->sectionSizeHint(i),
> tableView->sizeHintForColumn(i)));
>
> 2017-03-16 23:21 GMT+01:00 Etienne Sandré-Chardonnal <
> etienne.sandre at m4x.org>:
>
>> Hi,
>>
>> You can resize the header sections programmatically after the first load.
>>
>> for(int i=0;i<header->count();++i)
>>     header->resizeSection(i, header->sectionSizeHint(i));
>>
>> This should do the job.
>>
>> Cheers,
>>
>> Etienne
>>
>> 2017-03-16 22:27 GMT+01:00 Alexander Semke <alexander.semke at web.de>:
>>
>>> Hi,
>>>
>>> I have a QTreeView where after setting the model I call header()-
>>> >setResizeMode(QHeaderView::ResizeToContents) to adjust the column
>>> width to
>>> the content. Since I want the user to be able to resize the header
>>> sections
>>> manually, I call header()->setResizeMode(QHeaderView::Interactive)
>>> after this
>>> which squeezes the columns to the sizes where the content is not fully
>>> visible
>>> anymore. In my model in headerData() I don't calculate any sizes for the
>>> SizeHintRole case.
>>>
>>> How can I achieve this behaviour (tree view columns automatically
>>> adjusted to
>>> the content after the initial load and manually resizable later) in the
>>> most
>>> easiest way?
>>>
>>> --
>>> Alexander
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/interest
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170317/5598cb9d/attachment.html>


More information about the Interest mailing list