diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index e7be2339da..e89b914227 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -2665,13 +2665,14 @@ QByteArray QMetaEnum::valueToKeys(int value) const int count = mobj->d.data[handle + 2]; int data = mobj->d.data[handle + 3]; int v = value; - for(int i = 0; i < count; i++) { + // reverse iterate to ensure values like Qt::Dialog=0x2|Qt::Window are processed first. + for (int i = count - 1; i >= 0; --i) { int k = mobj->d.data[data + 2*i + 1]; if ((k != 0 && (v & k) == k ) || (k == value)) { v = v & ~k; if (!keys.isEmpty()) - keys += '|'; - keys += stringData(mobj, mobj->d.data[data + 2*i]); + keys.prepend('|'); + keys.prepend(stringData(mobj, mobj->d.data[data + 2*i])); } } return keys; diff --git a/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp b/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp index e81e105bfb..17b00ebf63 100644 --- a/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp +++ b/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp @@ -46,6 +46,8 @@ public: private slots: void fromType(); + void valuesToKeys_data(); + void valuesToKeys(); }; void tst_QMetaEnum::fromType() @@ -57,5 +59,37 @@ void tst_QMetaEnum::fromType() QCOMPARE(meta.keyCount(), 2); } +Q_DECLARE_METATYPE(Qt::WindowFlags) + +void tst_QMetaEnum::valuesToKeys_data() +{ + QTest::addColumn("windowFlags"); + QTest::addColumn("expected"); + + QTest::newRow("Window") + << Qt::WindowFlags(Qt::Window) + << QByteArrayLiteral("Window"); + + // Verify that Qt::Dialog does not cause 'Window' to appear in the output. + QTest::newRow("Frameless_Dialog") + << (Qt::Dialog | Qt::FramelessWindowHint) + << QByteArrayLiteral("Dialog|FramelessWindowHint"); + + // Similarly, Qt::WindowMinMaxButtonsHint should not show up as + // WindowMinimizeButtonHint|WindowMaximizeButtonHint + QTest::newRow("Tool_MinMax_StaysOnTop") + << (Qt::Tool | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint) + << QByteArrayLiteral("Tool|WindowMinMaxButtonsHint|WindowStaysOnTopHint"); +} + +void tst_QMetaEnum::valuesToKeys() +{ + QFETCH(Qt::WindowFlags, windowFlags); + QFETCH(QByteArray, expected); + + QMetaEnum me = QMetaEnum::fromType(); + QCOMPARE(me.valueToKeys(windowFlags), expected); +} + QTEST_MAIN(tst_QMetaEnum) #include "tst_qmetaenum.moc"