Results 1 to 2 of 2

Thread: QDateEdit Month/Day Rollover

  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    1
    Platforms
    Windows

    Default QDateEdit Month/Day Rollover

    Playing with an idea for month/day rollovers using QDateEdit. I realize the StepEnabled function is unsafe and subject to change between versions of Qt.

    The current version for this is 4.2.2.

    What I need this to do is if you increment the month past 12, it will step to the next year. If you decrement it past 1, it will step to the previous year. Same for the date, steps to the next month and if necessary, the next year. There will be no min date or max date defined.

    I'm just making sure that I didn't overlook something major (like an easy way to do this or a devastating bug) and looking for perhaps an easier or safer to do this.

    Please keep in mind that C++ is not my primary language so please be easy on the little nuances that I'm probably screwing up, though pointing them out is never bad (makes me a better programmer).

    What I ended up changing to create my custom widget...

    ###############################
    in the header
    ###############################
    Qt Code:
    1. class CustomQDateEdit : public QDateEdit
    2. {
    3. Q_OBJECT
    4. public:
    5. CustomQDateEdit(QWidget *parent = 0);
    6. CustomQDateEdit(QDate &date, QWidget *parent = 0);
    7. QDateTimeEdit::StepEnabled stepEnabled() const;
    8. void stepBy(int steps);
    9. };
    To copy to clipboard, switch view to plain text mode 
    ##########################
    in the source
    ##########################
    Qt Code:
    1. CustomQDateEdit::CustomQDateEdit(QWidget *parent) : QDateEdit( parent)
    2. {
    3. }
    4.  
    5. CustomQDateEdit::CustomQDateEdit(QDate &date, QWidget *parent) : QDateEdit(date, parent)
    6. {
    7. }
    8.  
    9. //this effectively disables step checking on QDateEdits
    10. //1 is a check for if it can step up, 2 is a check for if it can step down...or them together and get 3
    11. //do not use this if you want min and max ranges defined...unless you want it wrapping
    12. QDateTimeEdit::StepEnabled CustomQDateEdit::stepEnabled() const
    13. {
    14. return 3;
    15. }
    16.  
    17. //custom stepBy...this allows rollover of months and years on QDateEdits
    18. void CustomQDateEdit::stepBy(int steps)
    19. {
    20. QDate date(QDateEdit::date());
    21. QDateEdit::Section temp_section = currentSection();
    22. if(temp_section == QDateEdit::DaySection)
    23. {
    24. QDate next_date(date.addDays(steps));
    25. if(next_date.month() != date.month() || next_date.year() != date.year())
    26. {
    27. QDateEdit::setDate(next_date);
    28. steps = 0;
    29. }
    30. }
    31. else if(temp_section == QDateEdit::MonthSection)
    32. {
    33. QDate next_date(date.addMonths(steps));
    34. if(next_date.year() != date.year())
    35. {
    36. QDateEdit::setDate(next_date);
    37. steps = 0;
    38. }
    39. }
    40. QDateEdit::stepBy(steps);
    41. }
    To copy to clipboard, switch view to plain text mode 

    This works, as far as I can tell...
    Last edited by jpn; 7th January 2008 at 19:46. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDateEdit Month/Day Rollover

    One tiny little thing. I think you really should consider writing
    Qt Code:
    1. QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled
    To copy to clipboard, switch view to plain text mode 
    instead of "3" for the sake of readability and maintainability.
    J-P Nurmi

Similar Threads

  1. Problem with receiving events from QDateEdit
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 12:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.