I want to specify a QlineEdit that it can only enter an hour format like that (24:00); so it accepts only digits and its maximum is 24:00 where the initial value is 00:00 and when i modify the hour it automatically keep the ":". Thank you.
-
Can't you use a [QTimeEdit](https://doc.qt.io/qt-5/qtimeedit.html) instead? – musicamante Jun 30 '20 at 12:58
-
I can't because i couldn't enter this value " 24:00" it becomes "23:59" because i dont need it as an hour but as a time of using something – walid761 Jun 30 '20 at 14:09
3 Answers
The comments you made after the answer by Sven show that you need a much more robust and complete solution than you initally asked for. If you want to fully customize what kind of characters can/ can't be added into a LineEdit and want to run functions that test the inputs, you will need to look into using a QValidator.
In case you aren't quite sure what a QValidator is:
A QValidator is a class that provides validation of input text. When using one, you connect it to a QLineEdit, QSpinBox, or a QComboBox. Whenever the user is editing the connected field, after every character entered, it runs a
validate
function that can be implemented to run whatever logic you need to see if it is valid.
This link shows a solution to your problem using a QRegExpValidator. However, if you still need more checking than a RegEx can provide (or if you are uncomfortable with using RegEx), you will need to subclass QValidator and implement the validate
function.

- 166
- 3
A possible solution is to use a QTimeEdit, with a small "hack".
QTime range is always limited to 00:00:00 -> 23:59:59, but since you only need hours and minutes, you can "fake" it by using minutes and seconds instead of hours and minutes. Obviously, if you also need to show seconds, this won't be possible.
The trick is to limit the time range to 00:00:00 -> 00:24:00 (note that now we have a maximum set to 24 minutes) and always convert from/to the correct formats.
I overwrote the time
and setTime
methods, so that you can easily get/set a QTime using the correct HH:MM format. The timeChanged
signal is also being "overwritten" in a similar fashion.
I also modified stepBy
and stepEnabled
methods, because by default step actions (wheel, arrow buttons, page/arrow keys) are limited to the section in which the cursor is: you couldn't go further HH:59 if the cursor is in the minute section; with my implementation this limitation is removed and you can easily go from 00:59 to 01:00.
class Time24Edit(QtWidgets.QTimeEdit):
minTime = QtCore.QTime(0, 0)
maxTime = QtCore.QTime(0, 24)
_timeChanged = QtCore.pyqtSignal(QtCore.QTime)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setDisplayFormat('mm:ss')
self.setMinimumTime(self.minTime)
self.setMaximumTime(self.maxTime)
# "substitute" the base timeChanged signal with the custom one so that
# we emit the correct HH:MM time
self._baseTimeChanged = self.timeChanged
self._baseTimeChanged.connect(self._checkTime)
self.timeChanged = self._timeChanged
def _checkTime(self, time):
self.timeChanged.emit(self.time())
def stepBy(self, step):
fakeTime = super().time()
seconds = fakeTime.second() + fakeTime.minute() * 60
if self.currentSection() == self.SecondSection:
seconds += step
elif self.currentSection() == self.MinuteSection:
seconds += step * 60
# "sanitize" the value to 0-1440 "minutes"
seconds = max(0, min(1440, seconds))
minutes, seconds = divmod(seconds, 60)
super().setTime(QtCore.QTime(0, minutes, seconds))
def stepEnabled(self):
fakeTime = super().time()
steps = 0
if fakeTime > self.minTime:
steps |= self.StepDownEnabled
if fakeTime < self.maxTime:
steps |= self.StepUpEnabled
return steps
def time(self):
# convert minutes/seconds to hours/minutes
fakeTime = super().time()
return QtCore.QTime(fakeTime.minute(), fakeTime.second())
def setTime(self, time):
# convert hours/minutes to minutes/seconds
super().setTime(QtCore.QTime(0, time.hour(), time.minute()))
if __name__ == '__main__':
def signalTest(time):
print(time)
import sys
app = QtWidgets.QApplication(sys.argv)
w = Time24Edit()
w.timeChanged.connect(signalTest)
w.show()
sys.exit(app.exec_())

- 41,230
- 6
- 33
- 58
You can use an "Input Mask".
qline = QLineEdit("00:00")
qline.setInputMask("HH:HH")
Remark
Your task is not completely defined. What shall happen if the format is violated? Overwrite the entry with a valid String? Do nothing? Send an error message? Is format already violated if a time >24h is entered?

- 1,277
- 12
- 19
-
i want when someone enter a letter or a symbol it won't be modified ! and if he entered a value bigger then 24:00 it becomes automatically 24:00 – walid761 Jun 30 '20 at 14:10