PDA

View Full Version : QDateEdit Resize for Changed Display Format?



ChrisW67
5th June 2009, 06:18
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?



#include <QtGui>

int main(int argc, char *argv[])
{

QApplication app(argc, argv);
QWidget widget;

// My locale, in case it has a bearing
QLocale::setDefault(QLocale(QLocale::English, QLocale::Australia));

// default date format (d/mm/yy), right size for format
QDateEdit *date1 = new QDateEdit(QDate::currentDate(), &widget);

// wider date format, set before widget show(), right format and size
QDateEdit *date2 = new QDateEdit(QDate::currentDate(), &widget);
date2->setDisplayFormat("dd-MMM-yyyy");

// Another wider date format but set after widget show()
QDateEdit *date3 = new QDateEdit(QDate::currentDate(), &widget);

QFormLayout *form = new QFormLayout;
form->addRow("Default", date1);
form->addRow("Wider 1", date2);
form->addRow("Wider 2", date3);

widget.setLayout(form);
widget.show();

date3->setDisplayFormat("dd-MMM-yyyy"); // changes format but not widget size
// How do I force the geometry to be rethought after the parent widget is shown?
// The date3 widget sizeHint() is updated by the change

return app.exec();
}

nish
5th June 2009, 07:53
call setMinimumSize or reimplement minimumSizeHint() :D(better)

ChrisW67
5th June 2009, 08:32
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.

sizeHint() before = QSize(90, 21)
sizeHint() after = QSize(114, 21)
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.

nish
5th June 2009, 08:45
the layout always respects the minimumSizeHint, but sizeHint() can be ignored

ChrisW67
5th June 2009, 09:14
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.

nish
5th June 2009, 10:02
looks like i need to go back to school:)