Fix DeferredFlag implementation for QTestEventLoop

As discovered in the expanded testing of QTRY_COMPARE() using the same
class, the timer needs a context object and a slot to call.
This amends commit 35ad157d88

Pick-to: 6.4 6.3
Task-number: QTBUG-104441
Change-Id: I41fc23de84ce8c7d6608db0005276a2071974494
Reviewed-by: Jason McDonald <macadder1@gmail.com>
This commit is contained in:
Edward Welbourne 2022-08-12 11:15:48 +02:00
parent 54bcefb25c
commit 45cbb1e31e

View File

@ -18,21 +18,29 @@ private slots:
void pass(); void pass();
}; };
class DeferredFlag class DeferredFlag : public QObject // Can't be const.
{ {
Q_OBJECT
bool m_flag; bool m_flag;
public: public:
// A boolean that either starts out true or decays to true after 50 ms.
// However, that decay will only happen when the event loop is run.
explicit DeferredFlag(bool initial = false) : m_flag(initial) explicit DeferredFlag(bool initial = false) : m_flag(initial)
{ {
if (!initial) if (!initial)
QTimer::singleShot(50, [this] { m_flag = true; }); QTimer::singleShot(50, this, &DeferredFlag::onTimeOut);
} }
explicit operator bool() const { return m_flag; } explicit operator bool() const { return m_flag; }
bool operator!() const { return !m_flag; } bool operator!() const { return !m_flag; }
friend bool operator==(DeferredFlag a, DeferredFlag b) { return bool(a) == bool(b); } friend bool operator==(const DeferredFlag &a, const DeferredFlag &b)
{
return bool(a) == bool(b);
}
public slots:
void onTimeOut() { m_flag = true; }
}; };
char *toString(DeferredFlag val) char *toString(const DeferredFlag &val)
{ {
return qstrdup(bool(val) ? "DeferredFlag(true)" : "DeferredFlag(false)"); return qstrdup(bool(val) ? "DeferredFlag(true)" : "DeferredFlag(false)");
} }