admin管理员组

文章数量:1122846

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()):
        # 
        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:

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

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:

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

Share Improve this question edited Nov 24, 2024 at 3:54 musicamante 48.1k8 gold badges40 silver badges71 bronze badges asked Nov 22, 2024 at 13:35 DagDag 871 silver badge6 bronze badges 12
  • What you see is the current locale you're using (or, to be precise, what Qt detects from the system). You could consider to change the default locale (QLocale.setDefault()) with one you know uses the dot for date/thousand separators and comma for decimals. Alternatively, use a custom delegate and override displayText(). – musicamante Commented Nov 22, 2024 at 14:13
  • Hi, I set already the QLocale to QLocale.system() but it does not affect this tableview. Where should I set the displayText()? I’m sorry but I do not have any experience in using custom delegates. It would be nice if you could share some experience and show some example that could help solve this problem. Thanks! – Dag Commented Nov 22, 2024 at 15:15
  • On Windows/Mac QLocale should use the formats set for the system by default. I assume that you're using a German locale (probably de_DE) which does by default use dots for date/groups and comma for decimal point (at least on Linux), unless the system is configured differently. What is the output of QLocale.system().uiLanguages(), QLocale.system().dateFormat(QLocale.FormatType.ShortFormat) and QLocale.system().decimalPoint()? – musicamante Commented Nov 22, 2024 at 15:34
  • The output is: UI Lang: ['de-DE', 'de-Latn-DE', 'de'] Date format: dd.MM.yyyy DEzimal: , This is getting out the right formats but the PySide Widgets do not get it right seems so – Dag Commented Nov 22, 2024 at 16:19
  • Or could it be the case that the Sqlite DB gives the wrong "information" to the TableView? I set the fields as DATE and DOUBLE (10,2) – Dag Commented Nov 22, 2024 at 16:25
 |  Show 7 more comments

1 Answer 1

Reset to default -1

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'))

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

本文标签: sqlitePython Pyside6 QtableView Data model display data in locale formatStack Overflow