QLineEdit: take text margins into account in minimumSizeHint().

sizeHint() did it exactly like this, but minimumSizeHint() didn't,
which made it too small. Didn't affect the actual size in most cases
since the vertical size policy is fixed, so sizeHint() is called instead.
But when writing a subclass, if one re-implements sizeHint() by
calling the QLineEdit's minimumSizeHint(), it would then be wrong,
when text margins are used.

Change-Id: I29ae8dcab00842b3b5ca534cdb250efc0b496f45
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
bb10
David Faure 2014-09-25 17:23:15 +02:00 committed by Konstantin Ritt
parent b9d98c10bd
commit 2d0b20ef92
2 changed files with 14 additions and 1 deletions

View File

@ -712,8 +712,11 @@ QSize QLineEdit::minimumSizeHint() const
ensurePolished();
QFontMetrics fm = fontMetrics();
int h = fm.height() + qMax(2*d->verticalMargin, fm.leading())
+ d->topTextMargin + d->bottomTextMargin
+ d->topmargin + d->bottommargin;
int w = fm.maxWidth() + d->leftmargin + d->rightmargin;
int w = fm.maxWidth()
+ d->effectiveLeftTextMargin() + d->effectiveRightTextMargin()
+ d->leftmargin + d->rightmargin;
QStyleOptionFrame opt;
initStyleOption(&opt);
return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h).

View File

@ -3384,10 +3384,20 @@ void tst_QLineEdit::textMargin()
testWidget.setCursorPosition(6);
QSize sizeHint = testWidget.sizeHint();
QSize minSizeHint = testWidget.minimumSizeHint();
testWidget.setTextMargins(left, top, right, bottom);
sizeHint.setWidth(sizeHint.width() + left + right);
sizeHint.setHeight(sizeHint.height() + top +bottom);
QCOMPARE(testWidget.sizeHint(), sizeHint);
if (minSizeHint.width() > -1) {
minSizeHint.setWidth(minSizeHint.width() + left + right);
minSizeHint.setHeight(minSizeHint.height() + top + bottom);
QCOMPARE(testWidget.minimumSizeHint(), minSizeHint);
}
testWidget.setFrame(false);
centerOnScreen(&tlw);
tlw.show();