Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:18:30+00:00 2026-06-06T09:18:30+00:00

I’m trying to set up a custom sort behaviour for my model/view structure in

  • 0

I’m trying to set up a custom sort behaviour for my model/view structure in PySide.
All items are dictionaries and I need to filter by their keys (I’m not after a table view of those dictionaries but rather need to present them as one entity each).

I inherited from QSortFilterProxyModel and re-implemented the lessThan method. It works fine the first time the sort widget is changed (which triggers the proxy’s sort() method), but after that the lessThan method is no longer called. I have no idea why and an hoping to get some help here. I’m more than happy to consider any suggestions how to tackle this in a different way if that is where the solution lies.

This is my proxy:

class ProxyModel ( QSortFilterProxyModel ):
    def __init__( self, parent=None ):
        super( ProxyModel, self).__init__( parent )
        self.setFilterCaseSensitivity( Qt.CaseInsensitive )
        self.setSortCaseSensitivity( Qt.CaseInsensitive )
        self.setDynamicSortFilter( True )

    def sortBy( self, attr ):
        print 'sorting by', attr
        self.__sortBy = attr
        self.sort( 0, Qt.AscendingOrder ) # THIS DOES NOT GET CALLED WHEN THE COMBO BOX CHANGES A SECOND TIME

    def lessThan( self, left, right ):
        '''Custom sorting behaviour'''
        leftTool = ( self.sourceModel().itemFromIndex( left ) )
        rightTool = ( self.sourceModel().itemFromIndex( right ) )
        leftData = leftTool.data()[ self.__sortBy ]
        rightData = rightTool.data()[ self.__sortBy ]
        return leftData < rightData

And here is the complete test code:
import sys
from PySide.QtGui import *
from PySide.QtCore import *

class MainWidget( QWidget ) :
    def __init__( self, parent=None ):
        super( MainWidget, self ).__init__()

        self.listView = MyListView()
        model = MyModel()

        # MODELS AND VIEWS
        self.proxyModel = ProxyModel()
        self.proxyModel.setSourceModel( model )
        self.listView.setModel(self.proxyModel)

        # LAYOUTS
        verticalLayout = QVBoxLayout()
        filterLayout = QHBoxLayout()

        # SORTING WIDGET
        sortLayout = QHBoxLayout()
        sortLabel = QLabel( 'sort:' )
        self.sortWidget = QComboBox()
        self.sortWidget.addItems( ['title', 'author', 'downloads'] )
        self.sortWidget.currentIndexChanged.connect( self.sortTools )

        sortLayout.addWidget( sortLabel )
        sortLayout.addWidget( self.sortWidget )

        verticalLayout.addLayout( filterLayout )
        verticalLayout.addLayout( sortLayout )
        verticalLayout.insertWidget(0, self.listView)

        self.setLayout( verticalLayout )

    def sortTools( self ):
        text = self.sortWidget.currentText()
        self.proxyModel.sortBy( text )

class ProxyModel ( QSortFilterProxyModel ):
    def __init__( self, parent=None ):
        super( ProxyModel, self).__init__( parent )
        self.setFilterCaseSensitivity( Qt.CaseInsensitive )
        self.setSortCaseSensitivity( Qt.CaseInsensitive )
        self.setDynamicSortFilter( True )

    def sortBy( self, attr ):
        print 'sorting by', attr
        self.__sortBy = attr
        self.sort( 0, Qt.AscendingOrder ) # THIS DOES NOT GET CALLED WHEN THE COMBO BOX CHANGES A SECOND TIME

    def lessThan( self, left, right ):
        '''Custom sorting behaviour'''
        leftTool = ( self.sourceModel().itemFromIndex( left ) )
        rightTool = ( self.sourceModel().itemFromIndex( right ) )
        leftData = leftTool.data()[ self.__sortBy ]
        rightData = rightTool.data()[ self.__sortBy ]
        return leftData < rightData


class MyListView( QListView ):
    def __init__( self, parent=None ):
        super( MyListView, self).__init__( parent )
        self.setEditTriggers( QListView.NoEditTriggers )
        self.setViewMode( QListView.IconMode )
        self.setMovement( QListView.Static )
        self.setResizeMode( QListView.Adjust )
        self.setDragEnabled( True )


class MyModel( QStandardItemModel ):
    def __init__( self, parent=None ):
        super( MyModel, self).__init__( parent )
        self.init_data()

    def init_data(self):
        row = 0
        toolData = [ {'title':'ToolA', 'author':'John Doe', 'downloads':123, 'category':'color'},
                     {'title':'ToolB', 'author':'me', 'downloads':13, 'category':'color'},
                     {'title':'ToolC', 'author':'you', 'downloads':321, 'category':'transform'},
                     {'title':'ToolD', 'author':'unknown', 'downloads':2, 'category':'transform'}]

        for tool in toolData:
            item = QStandardItem( '%(title)s by %(author)s (%(category)s) - %(downloads)s downloads' % tool )
            item.setData( tool )
            self.setItem( row, 0, item )
            row += 1




if __name__ == '__main__':
    app = QApplication( sys.argv )

    mainWidget = MainWidget()
    mainWidget.resize( 400, 400 )
    mainWidget.show()

    sys.exit( app.exec_() )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:18:32+00:00Added an answer on June 6, 2026 at 9:18 am

    Basically Qt “thinks” your Proxy is already sorted. To be precise it checks in C++ whether:

    (d->dynamic_sortfilter && 
     d->proxy_sort_column == column && 
     d->sort_order == order)
    

    So, to solve your problem, you can either set dynamic_sortfilter to False (it will have side-effects) or, maybe better, invalidate your sorting:

    def sortBy( self, attr ):
        print 'sorting by', attr
        self.__sortBy = attr
        self.invalidate() #invalidate helps 
        self.sort( 0, Qt.AscendingOrder )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.