[PyQt] QTreeView and sorting

dizou di_zou at yahoo.com
Thu Nov 18 14:00:30 GMT 2010


So this is what I did:

class TreeWidget(QTreeWidgetItem):
    def __init__(self, parent=None):
        QTreeWidgetItem.__init__(self, parent)
    def key(self, col, asc):
        print col
        if col == 4:
            path = self.text(col).latin1()
            if path is not None and path.find("[") != -1:
                pathIndex = path[path.rfind('[') + 1:len(path) - 1]
                for i in range(20 - len(pathIndex)):
                    pathIndex = '0' + pathIndex
                path = self.GetObjectType() + pathIndex
                print path
                return path
            else:
                return self.text(col)
        else:
            return self.text(col)

THen when I sort column 4 in my application, nothing changes and nothing is
printed.

Reinaldo de Carvalho wrote:
> 
> On Wed, Nov 17, 2010 at 1:42 PM, dizou <di_zou at yahoo.com> wrote:
>>
>> I am using a QTreeView and I have sorting enabled. Sorting works great,
>> but I
>> want one column to be sorted a specific way that isn't how Qt does it by
>> default. I took a look at QSortFilterProxyModel, but this class looks
>> more
>> like a way to sort and filter data before the data is displayed in the
>> treeview. I am looking to sort the data a specific way after the data is
>> already in the tree view and after the user requests it to be sorted that
>> way. How would I do this?
> 
> You need change (virtually) the values at reimplement QListViewItem
> key(), two examples:
> 
> class QListViewItemQueueSorted(QListViewItem):
>   def __init__(self, *args):
>     QListViewItem.__init__(self, *args)
> 
>   def key(self, col, asc):
>     if col == 0:
>         # order email by domain
>         text = self.text(col).latin1()
>         # have't @ first (add 0 at left)
>         if text.find("@") == -1:
>             return "0%s" % text
>         # change domain to begin of string
>         else:
>             textList = text.split("@")
>             return "%s@%s" % (textList[1], textList[0])
>     elif col == 1:
>         # inserting zero's at left to have a 'number column sorting'
> fixing 20 caracters.
>         countText = self.text(col).latin1()
>         if countText is not None and countText.find("/") != -1:
>             countList = countText.split("/")
>             return "%s%s/%s%s" % ("0" * (20 - len(countList[1])),
> countList[1], "0" * (20 - len(countList[0])), countList[0])
>         else:
>             return self.text(col)
>     else:
>         return self.text(col)
> 
> 
> 
> -- 
> Reinaldo de Carvalho
> http://korreio.sf.net
> http://python-cyrus.sf.net
> 
> "While not fully understand a software, don't try to adapt this
> software to the way you work, but rather yourself to the way the
> software works" (myself)
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 
> 

-- 
View this message in context: http://old.nabble.com/QTreeView-and-sorting-tp30238745p30247666.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list