Fix QTest::toString() over QT_TYPESAFE_FLAGS QFlags

QT_TYPESAFE_FLAGS allows explicit casts only to QFlags<T>::Int, which
is either int or unsigned int. The cast to the resp. other type fails.

To fix, first convert to QFlags<T>::Int with toInt(), and only then
cast to int or unsigned int.

Fixes: QTBUG-101399
Pick-to: 6.3
Change-Id: Ie74d53adc601cdf19708265b040092780676058f
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
bb10
Marc Mutz 2022-03-03 00:11:13 +01:00
parent 8e56a7bc6b
commit e93bf391fa
1 changed files with 2 additions and 2 deletions

View File

@ -290,7 +290,7 @@ namespace QTest
inline typename std::enable_if<QtPrivate::IsQEnumHelper<F>::Value, char*>::type toString(QFlags<F> f)
{
const QMetaEnum me = QMetaEnum::fromType<F>();
return qstrdup(me.valueToKeys(int(f)).constData());
return qstrdup(me.valueToKeys(int(f.toInt())).constData());
}
template <typename F> // Fallback: Output hex value
@ -298,7 +298,7 @@ namespace QTest
{
const size_t space = 3 + 2 * sizeof(unsigned); // 2 for 0x, two hex digits per byte, 1 for '\0'
char *msg = new char[space];
qsnprintf(msg, space, "0x%x", unsigned(f));
qsnprintf(msg, space, "0x%x", unsigned(f.toInt()));
return msg;
}