以下是示例代码:
date = "1-Jan-2020"
widget_date = QtWidgets.QDateEdit()
widget_date .setDisplayFormat("d-MMM-yyyy")
widget_date .setDate(QDate.fromString(date))我想将该日期设置为QtWidgets.QDateEdit()。但它将默认日期设置为2000年1月1日
发布于 2020-08-13 01:01:43
你混淆了概念,setDisplayFormat()建立了文本如何在小部件中显示的格式,并且字符串到QDate的转换没有任何影响:
from PyQt5 import QtCore, QtWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
date_str = "1-Jan-2020"
# convert str to QDate
qdate = QtCore.QDate.fromString(date_str, "d-MMM-yyyy")
widget_date = QtWidgets.QDateEdit()
# Set the format of how the QDate will be displayed in the widget
widget_date.setDisplayFormat("d-MMM-yyyy")
widget_date.setDate(qdate)
widget_date.show()
sys.exit(app.exec_())发布于 2020-08-12 19:23:47
我认为这是可能的,因为你的日期是字符串格式。那么你可以使用widget_date.setDisplayFormat("%d-%b-%Y")吗?
https://stackoverflow.com/questions/63375259
复制相似问题