[PyQt] Resizing rows to contents after sort when QSortFilterProxyModel is used

Steve Waterbury waterbug at pangalactic.us
Tue Feb 16 05:11:08 GMT 2016


OMG!  Finally stumbled across the answer tonight while reading about
QAbstractItemModel ... it has a signal I hadn't noticed before:
layoutChanged() -- all I had to do was connect that signal of
my QSortFilterProxyModel to my tableview's resizeRowsToContents,
boom, done!  Table view is pretty after the sort -- woo!

Note to self:  RTFM (as always ... but in all fairness, it's a
*big* FM ... ;)

Hope this is useful to someone else.

Cheers,
Steve

On 02/12/2016 01:21 AM, Steve Waterbury wrote:
> On 02/12/2016 01:17 AM, Steve Waterbury wrote:
>> On 02/05/2016 04:25 PM, David Cortesi wrote:
>>>     From: Steve Waterbury <waterbug at pangalactic.us
>>>     Any ideas on this one?
>>>
>>>      > I'm using a QTableView with a QSortFilterProxyModel.
>>>      > When I first instantiate and populate the
>>>      > table view, I call resizeColumnToContents on each
>>>      > column, followed by resizeRowsToContents on the table
>>>      > view (there are some multi-line cells).
>>>      >
>>>      > However, the sizing to contents is lost when a sort is done
>>>
>>> You need to grab some signal that is emitted when the sort completes and
>>> the view has been completely repopulated with sorted data. (Sort
>>> indicator changed might not necessarily correspond to that.) My guess
>>> would be  modelReset. I *think* the way the sort filter proxy works is,
>>> it tells the view, modelAboutToBeReset, then modelReset, which makes the
>>> view repopulate itself by calling the data() method of the model.
>>
>> Seemed like a logical approach but doesn't work -- thanks anyway.
>> I guess I will have to implement the sort myself and refresh the
>> view.  I'm rather surprised that Qt doesn't handle this rather
>> simple scenario!
>>
>> Steve
>
> Oops -- re-sending to correct the subject ... a little list
> etiquette:  never reply to a "digest" message without fixing
> the subject!  ;)
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list