I want to test the “copy” functionality of my Qt widget by simulating emission of QKeySequence::Copy, but QTest::keyClick doesn’t accept QKeySequence (or QKeySequence::StandardKey):
void QTest::keyClick ( QWidget * widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 ) [static]
void QTest::keyClick ( QWidget * widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 ) [static]
I could just hardcode copy as Ctrl+C:
QTest::keyClick(widget, Qt::Key_C, Qt::ControlModifier)
However that would be rather brittle and might fail when testing my widget in other environments, so I’d prefer to use QKeySequence::Copy if possible.
I know I can use QKeySequence::QKeySequence(StandardKey) to find the primary binding, but I can’t see how to extract the key and modifier(s). Alternatively, is there some other way to get the same result?
Worked it out: the
operator[]returns a bitwise combination of aQt::Keyand theQt::KeyboardModifiersflags type (this doesn’t seem to be documented anywhere). So the way to extract the key and modifiers is to mask withQt::KeyboardModifierMask(also completely undocumented):