[PySide] custom table sorting

Frank Rueter | OHUfx frank at ohufx.com
Wed Apr 11 02:23:10 CEST 2012


Cool, thanks once again Aaron.
I will get back to the sortable table task in a bit and look at your 
example, right after getting a grip on my animated pages. Need to start 
picking my battles to do things properly. :)

Cheers,
frank

On 4/10/12 11:32 PM, Aaron Richiger wrote:
> Hello Frank
>
> Regarding your custom table sorting problem, there are at least two options:
>
> a) Using QTableWidget()
> Recommended for small, "stupid", static tables only. As soon as larger
> data, databases or manipulations (sorting, filtering, editing) appear,
> use option b)! But if you still want to go this way, I could imagine the
> following:
>       1) Store each row of the table in an appropriate datastructure,
> sort this datastructure
>       2) Remove all widgets from your TableWidgetItem
>       3) Refill the table from the sorted datastructure
>
> b) Using QTableModel()/ QTableView()/ Controller: (The following only
> covers the case without database. If you have an underlying database,
> similar solutions are provided by QSqlTableModel and others).
> I highly recommend this approach, but as other mentioned, it's a little
> bit harder to dive into, but if you are familiar with the MVC pattern
> from other languages or apps, it's not a too big deal. Checkboxes,
> comboboxes (QSqlTableModel) are already on board, if you need other
> widgets, just create them by using QStyledItemDelegate(). The same for
> sorting, filtering. The main work is not done any longer on the view,
> but in the model class (compared to option a)). I wrote a little example
> showing an MVC table with sortable checkboxes (but without custom
> delegates):
>
> http://pastebin.com/rw9ipbit
>
> It's far from completeness to keep the example short, but could be a
> starting or inspiration point.
>
> Have a nice day!
> Aaron
>
>
>> will do, thanks
>>
>> On 7/04/12 9:27 PM, Sebastian Elsner wrote:
>>> I recommend reading the appropriate chapters in  this book:
>>> http://www.qtrac.eu/pyqtbook.html
>>> and have a look this ressource: http://www.yasinuludag.com/blog/
>>> also download a version of PyQt, it has all original Qt examples
>>> (including the model view thingies) translated to python, which helps a
>>> lot and is a good starting point.
>>>
>>> Am 07.04.2012 02:00, schrieb Frank Rueter | OHUfx:
>>>> Hi Sebastian,
>>>>
>>>> thanks again for your help. I couldn't get the sorting to work even
>>>> though the widgets in each column were always the same type.
>>>> You are probably totally right that this won't be very scalable, I just
>>>> wanted to get a grip on it first before diving into the
>>>> QTableView/QTableModel solution, which I think I won't be able to avoid.
>>>> These tables can indeed grow very large.
>>>> I shall read up on this though and try and do it properly. ;)
>>>>
>>>>
>>>> Cheers,
>>>> frank
>>>>
>>>>
>>>> On 6/04/12 10:03 PM, Sebastian Elsner wrote:
>>>>> Hi,
>>>>>
>>>>> assuming the checkboxes and the combo boxes are different data types
>>>>> (bool, vs, string) and thus displayed in different columns, sorting
>>>>> should happen automatically when you setSortingEnabled(True). Please
>>>>> note that you have to disable sorting while inserting items because this
>>>>> may lead to stringe behaviour. Also if you want to display checkboxes
>>>>> these should be set by
>>>>> QTableWidgetItem.setFlags(QTablewidgetItem.flags() |
>>>>> Qt.ItemIsUSerCheckable)) and setCheckState()  rather than doing a
>>>>> QTableWidget.setCellWidget(), because this is way faster. The combooxes
>>>>> can be inserted with setCellWidget, but be aware that you may run into
>>>>> performance issues if you add a lot of them.
>>>>>
>>>>> Alternatively you could subclass the QTableWidgetItem and override the<
>>>>> operator to provide your own sorting, which will be - of course -
>>>>> solower because implemented in python.
>>>>>
>>>>> Finally when dealing with large datasets I would recommend going for the
>>>>> true model/view approach with QTableView, QTableModel and
>>>>> QStyledItemDelegate, but those are more hard to use.
>>>>>
>>>>> Cheers
>>>>>
>>>>> Sebastian
>>>>>
>>>>>
>>>>>      Am 06.04.2012 02:50, schrieb Frank Rueter | OHUfx:
>>>>>> Hi guys,
>>>>>>
>>>>>> I'm working on a table that has ItemWidgets (comboBoxes, checkBoxes etc)
>>>>>> in some cells and I'm trying to figure out how to write a custom sort
>>>>>> behaviour that will enable me to sort the table by columns containing
>>>>>> widgets using the respective widgets' current value (e.g. show all rows
>>>>>> with a checked checkBox first, followed by all rows with an unchecked
>>>>>> checkBox).
>>>>>>
>>>>>> Has anyone done this before?
>>>>>> Any pointers would be fabulous!
>>>>>>
>>>>>> Cheers,
>>>>>> frank
>>>>>>
>>>>>> _______________________________________________
>>>>>> PySide mailing list
>>>>>> PySide at qt-project.org
>>>>>> http://lists.qt-project.org/mailman/listinfo/pyside
>>>>> _______________________________________________
>>>>> PySide mailing list
>>>>> PySide at qt-project.org
>>>>> http://lists.qt-project.org/mailman/listinfo/pyside
>>>> _______________________________________________
>>>> PySide mailing list
>>>> PySide at qt-project.org
>>>> http://lists.qt-project.org/mailman/listinfo/pyside
>>> _______________________________________________
>>> PySide mailing list
>>> PySide at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/pyside
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/pyside
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside



More information about the PySide mailing list