Python Forum
Sorting numerical values provided by QAbstractTableModel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting numerical values provided by QAbstractTableModel
#1
Hi,

I have subclassed QAbstractTableModel, and overwrote its data method like this:
    def data(self, index, role):
        if role == Qt.ItemDataRole.DisplayRole:
            value = self._daten.iloc[index.row(), index.column()]
            if isinstance(value, float): return "%.2f" % value
            if isinstance(value, str): return "%s" % value
            return str(value)
=> as you can see the method will always return a string. I figured out, if it's not a string, it will not be displayed in the QTableView.
But when I do now a sorting by using QSortFilterProxyModel numbers are now treaded like strings and the sorting is done on an alphanumerical basis.

self.model = mein_TableModel(daten)
self.proxymodel = QtCore.QSortFilterProxyModel() # <<-- I know, I should also use a subclassed QSortFilterProxyModel... but can't find a working example
self.proxymodel.setSourceModel(self.model)
self.FundamentalTabelle.setSortingEnabled(True)
self.FundamentalTabelle.setModel(self.proxymodel)
Sub-classing QSortFilterProxyModel and overriding it's lessThan method seems to be the right way. But all examples I found, resulted in new problems.
I even would not know, which of those examples I should post here to start with. Perhaps if somebody could direct me to a "golden-sample". Thanks!
(btw: my table has columns with text and columns with floats ... would be great if sorting could work on both types).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  restrict user input to numerical values MCL169 2 940 Apr-08-2023, 05:40 PM
Last Post: MCL169
Question Inserting Numerical Value to the Element in Optionlist and Printing it into Entry drbilgehanbakirhan 1 836 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,650 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  How to plot 3D graph of non numerical value? Gevni 0 2,238 Mar-05-2021, 02:50 PM
Last Post: Gevni
  sorting alphanumeric values in a human way idiotonboarding 3 2,649 Jan-22-2021, 05:57 PM
Last Post: idiotonboarding
  change numerical values to categorical names JoeOpdenaker 3 2,965 Nov-02-2020, 01:32 PM
Last Post: DeaD_EyE
  "socket.gaierror: [Errno 8] nodename nor servname provided, or not known" tucktuck9 0 4,450 May-02-2020, 02:06 AM
Last Post: tucktuck9
  Filtering Excel Document Data Based On Numerical Values eddywinch82 30 10,935 Feb-25-2020, 06:08 PM
Last Post: eddywinch82
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,102 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  Sorting values calculated in python stumunro 4 3,989 Sep-13-2017, 06:09 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020