Fix sorting for floats

This commit is contained in:
Andreas Tsouchlos 2024-01-04 22:21:00 +01:00
parent ab1a1c3aad
commit 6a497c8575
2 changed files with 27 additions and 5 deletions

View File

@ -44,7 +44,8 @@ class MainWindow(QMainWindow):
self._proxyModel = QSortFilterProxyModel(self)
self._proxyModel.setSourceModel(self._pandas_model)
self._table_view.setModel(self._proxyModel)
self._proxyModel.setDynamicSortFilter(False)
self._proxyModel.setDynamicSortFilter(True)
self._proxyModel.setSortRole(Qt.ItemDataRole.EditRole)
# Set event handlers
@ -67,7 +68,9 @@ class MainWindow(QMainWindow):
self._table_view.selectionModel().selectionChanged.connect(
self._handle_table_selection_changed)
#
# Table data updates
#
def set_statement_data(self, df: pd.DataFrame):
self._pandas_model.set_dataframe(df)
@ -96,7 +99,9 @@ class MainWindow(QMainWindow):
len(col))
self._table_view.setColumnWidth(i, max_char * 10)
#
# List data updates
#
def _add_categories(self, categories: typing.Sequence[str]):
for category in categories:
@ -113,7 +118,9 @@ class MainWindow(QMainWindow):
self._add_categories([category for category
in missing if category != ' '])
#
# Warnings
#
def _add_warning_item(self, text: str):
warning_item = WarningItem(text=text, parent=self)
@ -143,7 +150,9 @@ class MainWindow(QMainWindow):
"The column 'balance' does not exist. Please rename the column"
" containing the balance after each transaction to 'balance'")
#
# Event handlers
#
def _handle_header_right_click(self, pos):
column = self._table_view.horizontalHeader().logicalIndexAt(pos)
@ -192,6 +201,7 @@ class MainWindow(QMainWindow):
df = self._pandas_model.get_dataframe()
df.loc[row_indices, 'category'] = category
self._pandas_model.set_dataframe(df)
def _handle_apply_click(self):
@ -210,7 +220,9 @@ class MainWindow(QMainWindow):
df = self.get_statement_data()
df.to_csv(filename, index=False)
#
# Enable / Disable buttons
#
def _check_enable_delete_button(self):
if len(self._list_widget.selectedItems()) > 0:

View File

@ -1,6 +1,7 @@
import numpy
import pandas as pd
from PyQt6 import QtCore
from PyQt6.QtCore import Qt
from PyQt6.QtCore import Qt, QModelIndex
def _get_str_dataframe(df: pd.DataFrame) -> pd.DataFrame:
@ -32,11 +33,20 @@ class PandasModel(QtCore.QAbstractTableModel):
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if not index.isValid():
return QtCore.QVariant()
if role != Qt.ItemDataRole.DisplayRole:
if (role != Qt.ItemDataRole.DisplayRole) and (
role != Qt.ItemDataRole.EditRole):
return QtCore.QVariant()
item = self._data_str.iloc[index.row(), index.column()]
return QtCore.QVariant(str(item))
if role == Qt.ItemDataRole.DisplayRole:
item = self._data_str.iloc[index.row(), index.column()]
return QtCore.QVariant(item)
elif role == Qt.ItemDataRole.EditRole:
item = self._data.iloc[index.row(), index.column()]
if type(item) is numpy.float64:
return QtCore.QVariant(float(item))
else:
return QtCore.QVariant(item)
def headerData(self, section, orientation,
role=Qt.ItemDataRole.DisplayRole):