[SOLVED] QTimeEdit - update when focus out



  • I have a small Ui problem that user are complaining about.
    They would like to be able to only input 1 digit for the QTimeEdit (the 0 should be filled automatically before it). I agree that it's not natural to type "05" minutes and typing "5" in the editor should be converted to "05".

    Here is the problem explained with screenshot :
    https://www.dropbox.com/s/orxos1s4cp7lqn1/1digitUi.png

    If the user enter "5" and press enter, it is converted to "05" correctly
    But most of the time, user type "5" then they go out of the editor to click something else, and the value is lost and reverted to it's previous one (that's the problem i'm having, I would like focus out to behave like Enter is pressed on the editor)

    Thanks!

    [EDIT: I have tested another QTimeEdit not inside a QTableView and i'm not having this problem, the 0 is added when 1 digit is typed.. weird]

    More details and code here:

    @QWidget *IntervalDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

    {

    /// Duration;
    if (index.column() == 1 ) {
        QTimeEdit *editor = new QTimeEdit(parent);
        editor->setDisplayFormat("mm:ss");
        return editor;
    }@

  • Lifetime Qt Champion

    Hi,

    Do you also have a custom setModelData ? If not you could add one which ensures that you have the correctly formatted data getting in your model.

    Hope it helps



  • Hi SGaist,

    Yes I also have a custom setModelData, the problem is not that I get bad formatted data in my model, it is more like the opposite, the QTimeEdit is too difficult and reject time imputed with only one digit followed by a loose focus on the editor..
    Maybe I could catch the loose focus event on the QTimeEdit editor and emulate enter pressed, because when user press enter, the data get updated even with 1 digit inputted, I don't know if that made sense, the image in the previous post explains it if not :)

    Here is my setModelData method:

    @void IntervalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {

    /// Duration
    if (index.column() == 1) {
        QTimeEdit *timeEdit = static_cast<QTimeEdit*>(editor);
        QTime timeNow = timeEdit->time();
        qDebug() << "got here timeEdit" << timeEdit->time();  /// This show the last edited time, the inputed time got ignored by QTimeEdit?
        model->setData(index, timeNow, Qt::EditRole);
    }@


  • Solved with one line of code :)
    @timeEdit->interpretText();@

    Just have to add this line in setModelData, found in the spinBox delegate example, thank you!

    @void IntervalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {

    /// Duration
    if (index.column() == 1) {
        QTimeEdit *timeEdit = static_cast<QTimeEdit*>(editor);
        timeEdit->interpretText();
        QTime timeNow = timeEdit->time();
        model->setData(index, timeNow, Qt::EditRole);
    }@

  • Lifetime Qt Champion

    Nice !

    You're welcome !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.