From 9521a07af2da735849ce92df049569de9a55ad30 Mon Sep 17 00:00:00 2001 From: Laszlo Papp Date: Wed, 18 May 2022 12:06:53 +0200 Subject: QKeySequenceEdit: add a maximumSquenceLength property At the very least, it would be important to have a single combination key sequence. This is commonly seen in keyboard shortcut editors where QKeySequenceEdit is very much applicable. [ChangeLog][QtWidgets][QKeySequenceEdit] Added a maximumSquenceLength property. Done-with: Marc Mutz Change-Id: Id7fa5a8593eb150fa67d7e89308492c0a200ac36 Reviewed-by: Marc Mutz Reviewed-by: Volker Hilsheimer Reviewed-by: Qt CI Bot --- .../qkeysequenceedit/tst_qkeysequenceedit.cpp | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/widgets/qkeysequenceedit/tst_qkeysequenceedit.cpp b/tests/auto/widgets/widgets/qkeysequenceedit/tst_qkeysequenceedit.cpp index 14649fc345..d372505f8c 100644 --- a/tests/auto/widgets/widgets/qkeysequenceedit/tst_qkeysequenceedit.cpp +++ b/tests/auto/widgets/widgets/qkeysequenceedit/tst_qkeysequenceedit.cpp @@ -20,6 +20,7 @@ private slots: void testKeys_data(); void testKeys(); void testLineEditContents(); + void testMaximumSequenceLength(); }; void tst_QKeySequenceEdit::testSetters() @@ -49,6 +50,42 @@ void tst_QKeySequenceEdit::testKeys_data() QTest::newRow("4") << Qt::Key_N << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << QKeySequence("Ctrl+Shift+N"); } +void tst_QKeySequenceEdit::testMaximumSequenceLength() +{ + // + // GIVEN: + // - A QKeySequenceEdit with maxKeyCount == 1 + // - A QKeySequence with more than one key + // + QKeySequenceEdit edit; + edit.setMaximumSequenceLength(1); + QCOMPARE(edit.maximumSequenceLength(), 1); + + QKeySequence multi("Ctrl+X, S"); + QCOMPARE(multi.count(), 2); + + // + // WHEN: setting the key sequence on the edit + // + QTest::ignoreMessage(QtWarningMsg, + "QKeySequenceEdit: setting a key sequence of length 2 when " + "maximumSequenceLength is 1, truncating."); + edit.setKeySequence(multi); + + // + // THEN: + // - the maxKeyCount property doesn't change + // - the key sequence is truncated to maxKeyCount + // - and won't un-truncate by increasing maxKeyCount + // + QCOMPARE(edit.maximumSequenceLength(), 1); + const auto edited = edit.keySequence(); + QCOMPARE(edited.count(), 1); + QCOMPARE(edited, QKeySequence("Ctrl+X")); + edit.setMaximumSequenceLength(2); + QCOMPARE(edit.keySequence(), edited); +} + void tst_QKeySequenceEdit::testKeys() { QFETCH(Qt::Key, key); -- cgit v1.2.3