Results 1 to 6 of 6

Thread: QDateEdit Resize for Changed Display Format?

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Question QDateEdit Resize for Changed Display Format?

    Hi All,

    I'd like to be able to change the date format in QDateEdit widgets dynamically. QDateEdit::setDisplayFormat() changes the displayed format but does not cause the geometry of the widget to change to accommodate longer text.

    The example code generates three QDateEdits. The first two have their display format set before display and they size well. The third has its display format changed after display and stays at the default size (like the first widget). All this is on QT 4.5.1 on Linux.

    I have three questions:
    • Is it reasonable to expect the third editor to resize?
    • Is there a way to force a geometry rethink on the layout or parent widget?
    • Am I looking at a bug in Qt?

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5.  
    6. QApplication app(argc, argv);
    7. QWidget widget;
    8.  
    9. // My locale, in case it has a bearing
    10. QLocale::setDefault(QLocale(QLocale::English, QLocale::Australia));
    11.  
    12. // default date format (d/mm/yy), right size for format
    13. QDateEdit *date1 = new QDateEdit(QDate::currentDate(), &widget);
    14.  
    15. // wider date format, set before widget show(), right format and size
    16. QDateEdit *date2 = new QDateEdit(QDate::currentDate(), &widget);
    17. date2->setDisplayFormat("dd-MMM-yyyy");
    18.  
    19. // Another wider date format but set after widget show()
    20. QDateEdit *date3 = new QDateEdit(QDate::currentDate(), &widget);
    21.  
    22. QFormLayout *form = new QFormLayout;
    23. form->addRow("Default", date1);
    24. form->addRow("Wider 1", date2);
    25. form->addRow("Wider 2", date3);
    26.  
    27. widget.setLayout(form);
    28. widget.show();
    29.  
    30. date3->setDisplayFormat("dd-MMM-yyyy"); // changes format but not widget size
    31. // How do I force the geometry to be rethought after the parent widget is shown?
    32. // The date3 widget sizeHint() is updated by the change
    33.  
    34. return app.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 5th June 2009 at 06:23.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit Resize for Changed Display Format?

    call setMinimumSize or reimplement minimumSizeHint() (better)

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateEdit Resize for Changed Display Format?

    OK. Thanks for the suggestion.

    The size hint is already growing on date3 when the longer display format is set but, even though the sizePolicy on the editor is (Minimum, Fixed) (seems to be the default), the editor visual stays smaller than the hint.
    Qt Code:
    1. sizeHint() before = QSize(90, 21)
    2. sizeHint() after = QSize(114, 21)
    To copy to clipboard, switch view to plain text mode 
    It seems that I need to manually call QWidget::adjustSize() to have the visual size change. I expected the updateGeometry() method to alert the layout but this does not seem to happen. Judging from Designer-generated code it expects this too: no call to updateSize() is made after changing the display format.

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit Resize for Changed Display Format?

    the layout always respects the minimumSizeHint, but sizeHint() can be ignored

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateEdit Resize for Changed Display Format?

    If the sizeHint() can be totally ignored then the documentation for QSizePolicy::Policy is very misleading. For QSizePolicy::Minimum:
    The sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint().
    Emphasis is mine.

    I'll use the sizeHint() and force the minimum size for now.

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit Resize for Changed Display Format?

    looks like i need to go back to school

Similar Threads

  1. Replies: 1
    Last Post: 15th April 2009, 09:00
  2. How can you display binary data in hex format?
    By Cruz in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2009, 14:40
  3. how to display images in mosaic format?
    By Nithya in forum Qt Programming
    Replies: 14
    Last Post: 24th April 2008, 06:41
  4. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.