Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-34759

QDateTimeEdit setSelectedSection() not honoured

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4: Low
    • Resolution: Unresolved
    • Affects Version/s: 4.8.5, 5.1.0 , 5.1.1, 5.4.0 Alpha
    • Fix Version/s: None
    • Environment:
      Qt 4.8.5 (Gentoo dist), 5.1.0/5.1.1 (Qt Project binaries) on Linux 64bit (GCC 4.7)
      Qt 4.8.4 on Windows 32 bit (GCC 4.4)

      Description

      The QDateTimeEdit widget does not honour selectedSection() when shown.

      The example code should display the date time with the day section highlighted when the widget is shown. It shows the correct date time but the first section (year in this case) is the one that is highlighted. The return value from currentSection() == DaySection, and the correct text is returned if used in sectionText(). If the user starts typing the year section is the one that is edited.

      I have also tried deferring the setSelectedSection() call until after the show() with a zero timer. On Linux this results in no section being highlighted in the editor (no focus) although the DaySection is the current section. If you edit2->setFocus() its year section is highlighted.

      This may be related to long-closed bug QTBUG-11046

      main.cpp
      #include <QApplication>
      #include <QDateTimeEdit>
      #include <QVBoxLayout>
      #include <QTimer>
      #include <QDebug>
      
      class Widget: public QWidget {
          Q_OBJECT
          QDateTimeEdit *edit1;
          QDateTimeEdit *edit2;
      public:
          Widget(QWidget *p = 0): QWidget(p) {
              edit1 = new QDateTimeEdit(this);
              edit1->setDateTime(QDateTime(QDate(2013,11,12), QTime(13, 14, 0)));
              edit1->setDisplayFormat("yyyy-MM-dd hh:mm");
              edit1->setSelectedSection(QDateTimeEdit::DaySection);
      
              edit2 = new QDateTimeEdit(this);
              edit2->setDateTime(QDateTime(QDate(2013,11,12), QTime(13, 14, 0)));
              edit2->setDisplayFormat("yyyy-MM-dd hh:mm");
      
              QVBoxLayout *layout = new QVBoxLayout;
              layout->addWidget(edit1);
              layout->addWidget(edit2);
              setLayout(layout);
      
              QTimer::singleShot(0, this, SLOT(delayed()));
          }
      private slots:
          void delayed() {
              edit2->setSelectedSection(QDateTimeEdit::DaySection);
              qDebug() << edit1->currentSection() << edit1->sectionText(edit1->currentSection());
              qDebug() << edit2->currentSection() << edit2->sectionText(edit2->currentSection());
          }
      };
      
      int main(int argc, char **argv) {
          QApplication app(argc, argv);
          Widget w;
          w.show();
          return app.exec();
      }
      #include "main.moc"
      

      It appears that the focusInEvent() is resetting the currentSection() so I am working around this by subclassing QDateTimeEdit:

      class Edit: public QDateTimeEdit {
      public:
          Edit(QWidget *p): QDateTimeEdit(p) { }
      protected:
          void focusInEvent(QFocusEvent *event) {
              Section section = currentSection();
              QDateTimeEdit::focusInEvent(event);
              setSelectedSection(section);
          }
      };
      

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            peppe Giuseppe D'Angelo
            Reporter:
            chrisw67 Chris Williams
            Votes:
            2 Vote for this issue
            Watchers:
            10 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes