Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTimeEdit - update when focus out

[SOLVED] QTimeEdit - update when focus out

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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;
    }@
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        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);
        }@
        

        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #4

          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);
          }@
          

          Free Indoor Cycling Software - https://maximumtrainer.com

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Nice !

            You're welcome !

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved