Do not rely on the list of standard font sizes for QFontDialog.

QFontDialog should always show the correct size, even if the size
is not available in the list of standard font sizes. Native font
dialogs across common platforms show the correct size at all times.

Inside init() function, the size list requires proper initialization
since the initial value of size is zero.

Task-number: QTBUG-10317
Change-Id: Idc9e922ac95f797ac98bbf6c885e52828c4c48fd
Reviewed-by: Liang Qi <liang.qi@digia.com>
This commit is contained in:
Leonard Lee 2014-03-25 10:47:29 +01:00 committed by The Qt Project
parent c4d8734c50
commit 685f8fe550
2 changed files with 42 additions and 8 deletions

View File

@ -256,8 +256,10 @@ void QFontDialogPrivate::init()
}
updateFamilies();
if (familyList->count() != 0)
if (familyList->count() != 0) {
familyList->setCurrentItem(0);
sizeList->setCurrentItem(0);
}
// grid layout
QGridLayout *mainGrid = new QGridLayout(q);
@ -621,16 +623,13 @@ void QFontDialogPrivate::updateSizes()
QStringList str_sizes;
for(QList<int>::const_iterator it = sizes.constBegin(); it != sizes.constEnd(); ++it) {
str_sizes.append(QString::number(*it));
if (current == -1 && *it >= size)
if (current == -1 && *it == size)
current = i;
++i;
}
sizeList->model()->setStringList(str_sizes);
if (current == -1) {
// we request a size bigger than the ones in the list, select the biggest one
current = sizeList->count() - 1;
}
sizeList->setCurrentItem(current);
if (current != -1)
sizeList->setCurrentItem(current);
const QSignalBlocker blocker(sizeEdit);
sizeEdit->setText((smoothScalable ? QString::number(size) : sizeList->currentText()));
@ -750,7 +749,10 @@ void QFontDialogPrivate::_q_sizeChanged(const QString &s)
break;
}
const QSignalBlocker blocker(sizeList);
sizeList->setCurrentItem(i);
if (sizeList->text(i).toInt() == this->size)
sizeList->setCurrentItem(i);
else
sizeList->clearSelection();
}
_q_updateSample();
}

View File

@ -66,6 +66,7 @@ public slots:
void postKeyReturn();
void testGetFont();
void testSetFont();
void testNonStandardFontSize();
public slots:
void initTestCase();
@ -76,6 +77,7 @@ private slots:
void defaultOkButton();
void setFont();
void task256466_wrongStyle();
void setNonStandardFontSize();
private:
void runSlotWithFailsafeTimer(const char *member);
@ -203,8 +205,38 @@ void tst_QFontDialog::task256466_wrongStyle()
}
}
void tst_QFontDialog::setNonStandardFontSize()
{
runSlotWithFailsafeTimer(SLOT(testNonStandardFontSize()));
}
void tst_QFontDialog::testNonStandardFontSize()
{
QList<int> standardSizesList = QFontDatabase::standardSizes();
int nonStandardFontSize;
if (!standardSizesList.isEmpty()) {
nonStandardFontSize = standardSizesList.at(standardSizesList.count()-1); // get the maximum standard size.
nonStandardFontSize += 1; // the increment of 1 to mock a non-standard font size.
} else {
QSKIP("QFontDatabase::standardSizes() is empty.");
}
QFont testFont;
testFont.setPointSize(nonStandardFontSize);
bool accepted = false;
QTimer::singleShot(2000, this, SLOT(postKeyReturn()));
QFont resultFont = QFontDialog::getFont(&accepted, testFont,
QApplication::activeWindow(),
QLatin1String("QFontDialog - NonStandardFontSize Test"),
QFontDialog::DontUseNativeDialog);
QVERIFY(accepted);
if (accepted)
QCOMPARE(testFont.pointSize(), resultFont.pointSize());
else
QWARN("Fail using a non-standard font size.");
}
QTEST_MAIN(tst_QFontDialog)
#include "tst_qfontdialog.moc"