pythonstrftimeqtimeqtimeedit

Python PyQt5 - Convert QTime to python time object


I have to solve this error:

day_frequency_parameters = self.main_self.ui_scheduled_transmitions_create_window.daily_one_time_timeEdit.time().strftime('%H:%M:%S')
AttributeError: 'QTime' object has no attribute 'strftime'

where daily_one_time_timeEdit is a QTimeEdit object.

Is there any way to convert QTimeEdit or QTime to python time object?


Solution

  • You could create a time string from QTime

    time_str = self.main_self.ui_scheduled_transmitions_create_window.daily_one_time_timeEdit.time().toString('%H:%M:%S')
    

    and create a time object from it:

    import datetime
    
    time_str = "14:32:15"
    datetime_obj = datetime.datetime.strptime(time_str, '%H:%M:%S')
    time_obj = datetime_obj.time()
    print(type(datetime_obj), datetime_obj)
    print(type(time_obj), time_obj)
    

    output:

    <class 'datetime.datetime'> 1900-01-01 14:32:15
    <class 'datetime.time'> 14:32:15
    

    I'm sure you can also do this directly with msec, so you don't have to create a string first.