基本上有三种不同的对象,你可以在PyQt的使用:
的QDateTime接受其他两种类型。因此,您可以使用QDate实例定义QDateTime对象的日期,并且可以使用QTime完成相同的操作。
很明显,如果你想改变你需要使用QTime对象的时间。
下面是一些例子:
#create a QDateTimeEdit object
myDTE = QtGui.QDateTimeEdit()
#get current date and time
now = QtCore.QDateTime.currentDateTime()
#set current date and time to the object
myDTE.setDateTime(now)
#set date only
today = QtCore.QDate.currentDate()
myDTE.setDate(today)
#set time only
this_moment = QtCore.QTime.currentTime()
myDTE.setTime(this_moment)
#set an arbitrary date
some_date = QtCore.QDate(2011,4,22) #Year, Month, Day
myDTE.setDate(some_date)
#set an arbitrary time
some_time = QtCore.QTime(16,33,15) #Hours, Minutes, Seconds (Only H and M required)
myDTE.setTime(some_time)
#set an arbitrary date and time
someDT = QtCore.QDateTime(2011,4,22,16,33,15)
myDTE.setDateTime(someDT)
非常感谢dex19dt!这个样本真的很好,对我的水平的人更好。 – Katherina 2012-07-17 01:50:39