1

My QTimeEdit displays HH:mm. The MiniuteSection has 15 as the step. My QTimeEdit increments well. But when I want to decrement the minute, I can only change the time from xx:45 to xx:30 to xx:15 and xx-1:45. As you can see, the time xx:00 is just skiped. There is no way to make it decrement from xx:15 to xx:00 to xx-1:45. Does anyone have idea how to solve the problem?

class FiveteenMinuteTimeEdit(QtWidgets.QTimeEdit):
    def stepBy(self, steps): 
        if self.currentSection() == self.MinuteSection:
            QtWidgets.QTimeEdit.stepBy(self, steps*15)
            t = self.time()
            if t.minute() == 59 and steps >0:
                time = QtCore.QTime()
                time.setHMS(t.hour()+1,0,0)
                self.setTime(time)
            if t.minute() == 0 and steps <0:
                time = QtCore.QTime()
                time.setHMS(t.hour()-1,45,0)
                self.setTime(time)
        else:
            QtWidgets.QTimeEdit.stepBy(self, steps)
WJPING
  • 11
  • 5

1 Answers1

1

You only have to add 60 * 15 * step seconds, also for a better implementation you must enable the up and down arrows when the time shown is in the appropriate limits overriden stepEnabled() method.

from PyQt5 import QtWidgets


class FiveteenMinuteTimeEdit(QtWidgets.QTimeEdit):
    def stepBy(self, step):
        if self.currentSection() == QtWidgets.QDateTimeEdit.MinuteSection:
            self.setTime(self.time().addSecs(60 * 15 * step))
            return
        super(FiveteenMinuteTimeEdit, self).stepBy(step)

    def stepEnabled(self):
        if self.currentSection() == QtWidgets.QDateTimeEdit.MinuteSection:
            if self.minimumTime() < self.time() < self.maximumTime():
                return (
                    QtWidgets.QAbstractSpinBox.StepUpEnabled
                    | QtWidgets.QAbstractSpinBox.StepDownEnabled
                )
        return super(FiveteenMinuteTimeEdit, self).stepEnabled()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = FiveteenMinuteTimeEdit()
    w.show()
    sys.exit(app.exec_())

Update:

The following code allows the change from 00:00 to 23:45 and from 00:00 to 23:00.

from PyQt5 import QtWidgets


class FiveteenMinuteTimeEdit(QtWidgets.QTimeEdit):
    def stepBy(self, step):
        d = {
            QtWidgets.QTimeEdit.SecondSection: step,
            QtWidgets.QTimeEdit.MinuteSection: 60 * 15 * step,
            QtWidgets.QTimeEdit.HourSection: 60 * 60 * step,
        }
        seconds = d.get(self.currentSection(), 0)
        self.setTime(self.time().addSecs(seconds))
        if self.currentSection() == QtWidgets.QTimeEdit.MSecSection:
            self.setTime(self.time().addMSecs(step))
        elif self.currentSection() == QtWidgets.QTimeEdit.AmPmSection:
            super(FiveteenMinuteTimeEdit, self).stepBy(step)

    def stepEnabled(self):
        return (
            QtWidgets.QAbstractSpinBox.StepUpEnabled
            | QtWidgets.QAbstractSpinBox.StepDownEnabled
        )


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = FiveteenMinuteTimeEdit()
    w.setDisplayFormat("hh mm")
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you for this answer. Your code works crazily perfectly. It also solve the problem : let the time go back to 00:00 from 23:45. – WJPING Jul 12 '19 at 10:19
  • When I do as what you said, the time doesn' t change. It keeps displaying 00:00. I also don't need it go back to 11:00. – WJPING Jul 12 '19 at 10:30
  • @WJPING I wrote my question wrong, what I meant, let's say that the time shown is 0:00 and you press the down arrow when the currect section is hour so should I go back to 23:00 or should it stay at 0:00? I ask it because according to the logic of your first comment you should enable the backspace from 0:00 to 23:45 – eyllanesc Jul 12 '19 at 10:34
  • I see. Yes, ideally when the current section is hour and the current time is 00:00, when down arrow is pressed, the time should go back to 23:00. – WJPING Jul 12 '19 at 10:38
  • I got it. Thank you very much. – WJPING Jul 12 '19 at 11:08