在pyqt5 Qprinter中转换为日期格式
convert to date format in pyqt5 Qprinter
有没有一种方法可以转换为日期格式,将使用打印机和 QTextDocument 结果始终显示在图像中的日期和时间中?
这是我的代码
def handlePaintRequest(self, printer):
model_hjd = QSqlTableModel()
model_hjd.setTable('transactions')
date = str(self.dateEdit_10.text())
date_2 = str(self.dateEdit_14.text())
self.tableView_22.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
model_hjd.setSort(6, Qt.DescendingOrder)
self.tableView_22.setModel(model_hjd)
model_hjd.select()
filter_ft = "date_d BETWEEN '%s' AND '%s'" % (date, date_2)
model_hjd.setFilter(filter_ft)
self.tableView_22.setModel(model_hjd)
document = QTextDocument()
cursor = QTextCursor(document)
tableFormat = QTextTableFormat()
table = cursor.insertTable(model_hjd.rowCount() + 1, model_hjd.columnCount(), tableFormat)
myheaders = []
for i in range(0, model_hjd.columnCount()):
myheader = model_hjd.headerData(i, Qt.Horizontal)
cursor.insertText(myheader)
cursor.movePosition(QTextCursor.NextCell)
for row in range(0, model_hjd.rowCount()):
for col in range(0, model_hjd.columnCount()):
index = model_hjd.index( row, col )
cursor.insertText(str(index.data()))
cursor.movePosition(QTextCursor.NextCell)
document.print_(printer)
但是日期和时间的结果是这样的
您可以使用自定义函数来格式化 table 中不同类型的数据,例如
@staticmethod
def to_string(entry):
if isinstance(entry, (QtCore.QDate, QtCore.QTime, QtCore.QDateTime)):
return entry.toString(Qt.SystemLocaleShortDate)
else:
return str(entry)
def handlePaintRequest(self, printer):
....
cursor.insertText(self.to_string(index.data()))
....
有没有一种方法可以转换为日期格式,将使用打印机和 QTextDocument 结果始终显示在图像中的日期和时间中?
这是我的代码
def handlePaintRequest(self, printer):
model_hjd = QSqlTableModel()
model_hjd.setTable('transactions')
date = str(self.dateEdit_10.text())
date_2 = str(self.dateEdit_14.text())
self.tableView_22.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
model_hjd.setSort(6, Qt.DescendingOrder)
self.tableView_22.setModel(model_hjd)
model_hjd.select()
filter_ft = "date_d BETWEEN '%s' AND '%s'" % (date, date_2)
model_hjd.setFilter(filter_ft)
self.tableView_22.setModel(model_hjd)
document = QTextDocument()
cursor = QTextCursor(document)
tableFormat = QTextTableFormat()
table = cursor.insertTable(model_hjd.rowCount() + 1, model_hjd.columnCount(), tableFormat)
myheaders = []
for i in range(0, model_hjd.columnCount()):
myheader = model_hjd.headerData(i, Qt.Horizontal)
cursor.insertText(myheader)
cursor.movePosition(QTextCursor.NextCell)
for row in range(0, model_hjd.rowCount()):
for col in range(0, model_hjd.columnCount()):
index = model_hjd.index( row, col )
cursor.insertText(str(index.data()))
cursor.movePosition(QTextCursor.NextCell)
document.print_(printer)
但是日期和时间的结果是这样的
您可以使用自定义函数来格式化 table 中不同类型的数据,例如
@staticmethod
def to_string(entry):
if isinstance(entry, (QtCore.QDate, QtCore.QTime, QtCore.QDateTime)):
return entry.toString(Qt.SystemLocaleShortDate)
else:
return str(entry)
def handlePaintRequest(self, printer):
....
cursor.insertText(self.to_string(index.data()))
....