当我按下按钮时如何在QDateEdit中更改日期。
我已经尝试过使用setDate...。但是确实有效
def loadpac(self):
bazadate = cur.execute('SELECT * FROM caz')
for row in bazadate:
if row[0] == int(self.linenr.text()):
self.date.setDate(QtCore.QDate(int(row[1][6:]), int(row[1][3:5]), int(row[1][0:2])))
这不是代码,只是解释什么是row [1]
行[1] = 2019年3月20日
答案 0 :(得分:0)
bazadate = [
# row[0] row[1] row[n]
[ 42, '23/02/2019', ... ], # <--- row
...
]
QtCore.QDate(int(row[1][6:]), int(row[1][3:5]), int(row[1][0:2]))
QtCore.QDate( 2019 , 2, 23)
尝试一下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MyForm(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.dateEdit = QtWidgets.QDateEdit(self)
self.dateEdit.setDateTime(QtCore.QDateTime(QtCore.QDate(2019, 2, 23),
QtCore.QTime(0, 0, 0)))
self.dateEdit.setDate(QtCore.QDate(2019, 2, 23))
self.dateEdit.setDisplayFormat("dd/MM/yyyy")
self.label = QtWidgets.QLabel()
self.label.setText("{}".format(self.dateEdit.dateTime().toString('dd-MM-yyyy')))
self.button = QtWidgets.QPushButton('Show selected date.')
self.button.clicked.connect(self.onClick)
layout = QtWidgets.QGridLayout(self)
layout.addWidget(self.label, 0, 0, 1, 2)
layout.addWidget(QtWidgets.QLabel("Select a date:"), 1, 0)
layout.addWidget(self.dateEdit, 1, 1)
layout.addWidget(self.button, 2, 0, 1, 2)
def onClick(self):
firstDayText = '{}-01-01'.format(self.dateEdit.dateTime().toString('yyyy'))
firstDay = QtCore.QDateTime.fromString(firstDayText, "yyyy-MM-dd")
numDay = firstDay.daysTo(self.dateEdit.dateTime())
self.label.setText("Selected date {}, {} days have passed since the beginning of the year"
"".format(self.dateEdit.dateTime().toString('dd-MM-yyyy'), numDay))
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())