Dag
Dag

Reputation: 87

Python Pyside6 QtableView Data model display data in locale format

I have a problem displaying data coming from QtSql.QSqlRelationalTableModel().

here one function I use to build the data model:

# FUNCTION IN DATABASE MODULE
def incomeViewData(self):
    """
    data model for income table
    """
    self.model.setTable("income")
    """
    set relation to the category, cycle and fix to show the name instead the id
    """
    self.model.setRelation(3, QtSql.QSqlRelation("inc_category", "inc_cat_id", "inc_cat_name"))
    self.model.setRelation(4, QtSql.QSqlRelation("cycle", "cyc_id", "cycle_name"))
    self.model.setRelation(5, QtSql.QSqlRelation("var_fix", "var_id", "value"))
    """
    set the ability to have a combobox for the category when double clicked in the cell
    """
    # self.tbl_income.setItemDelegate(QtSql.QSqlRelationalDelegate())
    
    # FORMAT HEADER
    self.model.setHeaderData(0, Qt.Horizontal, "ID")
    self.model.setHeaderData(1, Qt.Horizontal, "Datum")
    self.model.setHeaderData(2, Qt.Horizontal, "Betrag")
    self.model.setHeaderData(3, Qt.Horizontal, "Kategorie")
    self.model.setHeaderData(4, Qt.Horizontal, "Turnus")
    self.model.setHeaderData(5, Qt.Horizontal, "Fix")
    self.model.setHeaderData(6, Qt.Horizontal, "Bemerkung")
    self.model.setSort(1, QtCore.Qt.AscendingOrder)
    return self.model # RETURNS MODEL TO CALLING FUNCTION

# CALLING FUNCTION
def incomeView(self):
    # data model for income table
    mod_income = dbc().incomeViewData()
    mod_income.select()
    
    self.ui.load_pages.tbl_income.setModel(mod_income)
    self.ui.load_pages.tbl_income.setColumnHidden(0, True)
    for i in range(mod_income.columnCount()):
        # https://stackoverflow.com/questions/69912374/how-to-set-a-pyside6-qtablewidget-column-width-to-15pt
        self.ui.load_pages.tbl_income.horizontalHeader().setMinimumSectionSize(175)
        self.ui.load_pages.tbl_income.horizontalHeader().setSectionResizeMode(i, QHeaderView.ResizeToContents)
        self.ui.load_pages.tbl_income.horizontalHeader().setStretchLastSection(True)

and here is my result:

enter image description here

How can I set the date and amount to be displayed in local format like date 14.02.2023 and amunt 2.341,64?

Upvotes: 2

Views: 119

Answers (1)

Dag
Dag

Reputation: 87

I think I found sometihing that my help me to get the results I want to have. https://overthere.co.uk/2012/07/29/using-qstyleditemdelegate-on-a-qtableview/

class DateFormatDelegate(QStyledItemDelegate):
    def __init__(self, date_format):
        QStyledItemDelegate.__init__(self)        
        self.date_format = date_format

def displayText(self, value, locale):
    # ORIGINAL RETURN
    # return value.toDate().toString(self.date_format)
    
    # MY RETURN
    return QDate.fromString(value, "yyyy-MM-dd").toString("dd.MM.yyyy")

then in my calling function I added this line

self.ui.load_pages.tbl_income.setItemDelegateForColumn(1, DateFormatDelegate('dd.MM.yyyy'))

enter image description here

Thanks @musicamante for geting me the right direction for research :)

Upvotes: -1

Related Questions