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 | Show 7 more comments1 Answer
Reset to default -1I 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
版权声明:本文标题:sqlite - Python Pyside6 QtableView Data model display data in locale format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303503a1931908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
QLocale.setDefault()
) with one you know uses the dot for date/thousand separators and comma for decimals. Alternatively, use a custom delegate and overridedisplayText()
. – musicamante Commented Nov 22, 2024 at 14:13de_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 ofQLocale.system().uiLanguages()
,QLocale.system().dateFormat(QLocale.FormatType.ShortFormat)
andQLocale.system().decimalPoint()
? – musicamante Commented Nov 22, 2024 at 15:34