Search code examples
qtqmlqtquick2qtquickcontrols

Time input field in QML


I need to implement a TimeEdit(HH:MM:SS) field in QML similar to QTimeEdit in QT C++. In QML I didn't find TimeEdit and I have implemented the control similar to TimeEdit using TextField and if I add inputMask then the Regular expression is not at all validated, Is there any way that I can achieve this? Following is the code.

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Time Edit")

    TextField{
        id:textEditTD
        text : ""
        inputMethodHints: Qt.ImhDigitsOnly

        inputMask: "dd:dd:dd; "
        validator: RegExpValidator { regExp: /^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):[0-5][0-9]$ / }

        width:100
        height:50
        background:Rectangle{
            color:"transparent"
            border.color: "red"
            border.width:2
            radius:(width * 0.05)
        }
    }
}

Solution

  • I found two ways to implement this :

    1) I made changes to Orient answer to meet my requirement and following is the change which works when backspace is pressed:

    virtual
    State validate(QString & input, int & pos) const Q_DECL_OVERRIDE
    {
        const QStringList parts = input.split(":");
        if (parts.length() != 3) {
            input = QStringLiteral("00:00:00");
        }
        else
        {
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
    
            //hours
            if(parts[0].toInt() > 23){
                hours = 23;
                pos +=1; //Increment the position
            }
            else{
                QString str = parts[0];
                if(str.contains(" ")){
                    str.replace(" ","0");
                }
                hours = str.toInt();
            }
    
            // Minutes
            if(parts[1].toInt() > 59){
                minutes = 59;
                pos +=1; //Increment the position
            }
            else{
                QString str = parts[1];
                if(str.contains(" ")){
                    str.replace(" ","0");
                }
                minutes = str.toInt();
            }
    
            //Seconds
            if(parts[2].toInt() > 59){
                seconds = 59;
                pos +=1; //Increment the position
            }
            else{
                QString str = parts[2];
                if(str.contains(" ")){
                    str.replace(" ","0");
                }
                seconds = str.toInt();
            }
    
            const QTime time{hours, minutes,seconds};
            Q_ASSERT(time.isValid());
            input = time.toString("hh:mm:ss");
        }
        return Acceptable;
    }
    

    2) By just changing the inputMask and RegExp which is very easy:

    inputMask: "99:99:99"       
           validator: RegExpValidator { regExp: /^([0-1\s]?[0-9\s]|2[0-3\s]):([0-5\s][0-9\s]):([0-5\s][0-9\s])$ / }