PyQT4 change qDateTimeEdit time value?

2019-08-01 04:58发布

问题:

got stuck on this part of my program.. and it seems i cant find a decent example of how to do this..

i got a QDateTimeEdit object i already set its displaying value to what my current system time is using

self.ui.dateTimeEdit.setDate(QDate.currentDate())

its output is for example is 7/16/2012 12:00:00 AM

Now my problem is.. i want to set 12:00:00 AM to 11:59:59 PM

how can i do this please?

thanks to anyone willing to spend time on my question.

回答1:

There are basically three different objects you can use in PyQt:

  • QDate

  • QTime

  • QDateTime

The QDateTime accepts the other two types. So you can define the date of a QDateTime Object using a QDate instance and the same can be done with QTime.

Obviously, if you're trying to change the time you need to use a QTime object.

Here are some examples:

#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)