QProgressDialog: don't require setValue(0) to be called.
1) everyone forgets to do so (proof: tst_qnetworkaccessmanager_and_qprogressdialog.cpp forgot too, which led to a valgrind warning, the elapsed timer was never started) 2) setValue(0) makes no sense if the progress dialog goes from 50 to 60, or any other non-zero minimum value. Fixed by starting the timer in the constructor (most code doesn't reuse progress dialogs, so this fixes the most common case), and by also starting the timer when calling setValue(minimum()) for well-behaved dialogs. setValue(0) special case kept for compatibility. Task-number: QTBUG-17427 Task-number: QTBUG-25316 [ChangeLog][Important Behavior Changes][QProgressDialog] The timer for estimating the duration of the progress dialog is now started in the constructor and in setValue(minimum()), as well as when calling setValue(0), as previously documented. Done-with: Marc Mutz <marc.mutz@kdab.com> Change-Id: Ia8f7fc677438749191b99074fc334eab652ea36f Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
3d8a10b960
commit
3c6f333634
@ -65,6 +65,7 @@ public:
|
||||
QProgressDialogPrivate() : label(0), cancel(0), bar(0),
|
||||
shown_once(false),
|
||||
cancellation_flag(false),
|
||||
setValue_called(false),
|
||||
showTime(defaultShowTime),
|
||||
#ifndef QT_NO_SHORTCUT
|
||||
escapeShortcut(0),
|
||||
@ -87,6 +88,7 @@ public:
|
||||
QTimer *forceTimer;
|
||||
bool shown_once;
|
||||
bool cancellation_flag;
|
||||
bool setValue_called;
|
||||
QElapsedTimer starttime;
|
||||
#ifndef QT_NO_CURSOR
|
||||
QCursor parentCursor;
|
||||
@ -123,6 +125,8 @@ void QProgressDialogPrivate::init(const QString &labelText, const QString &cance
|
||||
} else {
|
||||
q->setCancelButtonText(cancelText);
|
||||
}
|
||||
starttime.start();
|
||||
forceTimer->start(showTime);
|
||||
}
|
||||
|
||||
void QProgressDialogPrivate::layout()
|
||||
@ -598,6 +602,7 @@ void QProgressDialog::reset()
|
||||
d->bar->reset();
|
||||
d->cancellation_flag = false;
|
||||
d->shown_once = false;
|
||||
d->setValue_called = false;
|
||||
d->forceTimer->stop();
|
||||
|
||||
/*
|
||||
@ -636,7 +641,7 @@ int QProgressDialog::value() const
|
||||
\brief the current amount of progress made.
|
||||
|
||||
For the progress dialog to work as expected, you should initially set
|
||||
this property to 0 and finally set it to
|
||||
this property to QProgressDialog::minimum() and finally set it to
|
||||
QProgressDialog::maximum(); you can call setValue() any number of times
|
||||
in-between.
|
||||
|
||||
@ -651,8 +656,7 @@ int QProgressDialog::value() const
|
||||
void QProgressDialog::setValue(int progress)
|
||||
{
|
||||
Q_D(QProgressDialog);
|
||||
if (progress == d->bar->value()
|
||||
|| (d->bar->value() == -1 && progress == d->bar->maximum()))
|
||||
if (d->setValue_called && progress == d->bar->value())
|
||||
return;
|
||||
|
||||
d->bar->setValue(progress);
|
||||
@ -661,11 +665,13 @@ void QProgressDialog::setValue(int progress)
|
||||
if (isModal())
|
||||
QApplication::processEvents();
|
||||
} else {
|
||||
if (progress == 0) {
|
||||
if ((!d->setValue_called && progress == 0 /* for compat with Qt < 5.4 */) || progress == minimum()) {
|
||||
d->starttime.start();
|
||||
d->forceTimer->start(d->showTime);
|
||||
d->setValue_called = true;
|
||||
return;
|
||||
} else {
|
||||
d->setValue_called = true;
|
||||
bool need_show;
|
||||
int elapsed = d->starttime.elapsed();
|
||||
if (elapsed >= d->showTime) {
|
||||
@ -759,7 +765,7 @@ void QProgressDialog::setMinimumDuration(int ms)
|
||||
{
|
||||
Q_D(QProgressDialog);
|
||||
d->showTime = ms;
|
||||
if (d->bar->value() == 0) {
|
||||
if (d->bar->value() == d->bar->minimum()) {
|
||||
d->forceTimer->stop();
|
||||
d->forceTimer->start(ms);
|
||||
}
|
||||
|
@ -52,6 +52,7 @@ private Q_SLOTS:
|
||||
void cleanup();
|
||||
void autoShow_data();
|
||||
void autoShow();
|
||||
void autoShowCtor();
|
||||
void getSetCheck();
|
||||
void task198202();
|
||||
void QTBUG_31046();
|
||||
@ -68,30 +69,62 @@ void tst_QProgressDialog::autoShow_data()
|
||||
{
|
||||
QTest::addColumn<int>("min");
|
||||
QTest::addColumn<int>("max");
|
||||
QTest::addColumn<int>("delay");
|
||||
QTest::addColumn<int>("value"); // initial setValue call
|
||||
QTest::addColumn<int>("delay"); // then we wait for this long, and setValue(min+1)
|
||||
QTest::addColumn<int>("minDuration");
|
||||
QTest::addColumn<bool>("expectedAutoShow");
|
||||
|
||||
QTest::newRow("50_to_100_long") << 50 << 100 << 100 << true; // 50*100ms = 5s
|
||||
QTest::newRow("50_to_100_short") << 50 << 1 << 100 << false; // 50*1ms = 50ms
|
||||
// Check that autoshow works even when not starting at 0
|
||||
QTest::newRow("50_to_100_slow_shown") << 50 << 100 << 50 << 100 << 100 << true; // 50*100ms = 5s
|
||||
QTest::newRow("50_to_100_fast_not_shown") << 50 << 100 << 50 << 1 << 100 << false; // 1ms is too short to even start estimating
|
||||
QTest::newRow("50_to_60_high_minDuration_not_shown") << 50 << 60 << 50 << 100 << 2000 << false; // 10*100ms = 1s < 2s
|
||||
|
||||
// Check that setValue(0) still starts the timer as previously documented
|
||||
QTest::newRow("50_to_100_slow_0_compat") << 50 << 100 << 0 << 100 << 100 << true; // 50*100ms = 5s
|
||||
QTest::newRow("50_to_100_fast_0_compat") << 50 << 100 << 0 << 1 << 100 << false; // 1ms is too short to even start estimating
|
||||
QTest::newRow("50_to_60_high_minDuration_0_compat") << 50 << 60 << 0 << 100 << 2000 << false; // 10*100ms = 1s < 2s
|
||||
|
||||
// Check the typical case of starting at 0
|
||||
QTest::newRow("0_to_100_slow_shown") << 0 << 100 << 0 << 100 << 100 << true; // 100*100ms = 10s > 100ms
|
||||
QTest::newRow("0_to_10_slow_shown") << 0 << 10 << 0 << 100 << 500 << true; // 10*100ms = 1s > 0.5s
|
||||
QTest::newRow("0_to_10_high_minDuration_not_shown") << 0 << 10 << 0 << 100 << 2000 << false; // 10*100ms = 1s < 2s
|
||||
|
||||
// Check the special case of going via 0 at some point
|
||||
QTest::newRow("-1_to_1_slow_shown") << -1 << 1 << -1 << 200 << 100 << true; // 1*200ms = 200ms > 100ms
|
||||
QTest::newRow("-1_to_1_fast_not_shown") << -1 << 1 << -1 << 10 << 100 << false; // 10ms is too short to even start estimating
|
||||
QTest::newRow("-1_to_1_high_minDuration_not_shown") << -1 << 1 << -1 << 100 << 2000 << false; // 1*100ms = 100ms < 2s
|
||||
|
||||
QTest::newRow("0_to_100_long") << 0 << 100 << 100 << true; // 100*100ms = 10s
|
||||
QTest::newRow("0_to_10_short") << 0 << 10 << 100 << false; // 10*100ms = 1s
|
||||
}
|
||||
|
||||
void tst_QProgressDialog::autoShow()
|
||||
{
|
||||
QFETCH(int, min);
|
||||
QFETCH(int, max);
|
||||
QFETCH(int, value);
|
||||
QFETCH(int, delay);
|
||||
QFETCH(int, minDuration);
|
||||
QFETCH(bool, expectedAutoShow);
|
||||
|
||||
QProgressDialog dlg("", "", min, max);
|
||||
dlg.setValue(0);
|
||||
if (minDuration != dlg.minimumDuration())
|
||||
dlg.setMinimumDuration(minDuration);
|
||||
dlg.reset(); // cancel the timer started in the constructor,
|
||||
// in order to test for the setValue() behavior instead
|
||||
// See autoShowCtor() for the ctor timer check
|
||||
dlg.setValue(value);
|
||||
QThread::msleep(delay);
|
||||
dlg.setValue(min+1);
|
||||
QCOMPARE(dlg.isVisible(), expectedAutoShow);
|
||||
}
|
||||
|
||||
void tst_QProgressDialog::autoShowCtor()
|
||||
{
|
||||
QProgressDialog dlg;
|
||||
QVERIFY(!dlg.isVisible());
|
||||
QThread::msleep(dlg.minimumDuration());
|
||||
QTRY_VERIFY(dlg.isVisible());
|
||||
}
|
||||
|
||||
// Testing get/set functions
|
||||
void tst_QProgressDialog::getSetCheck()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user