The interfaces can imitate users’ operations.
QTest::keyClick QTest::keyPress QTest::mouseClick
QSignalSpy listens for signal emission, it is a list of QVariant lists.
It will not shield off signal to make slot invalid.
QCheckBox *box = ...; QSignalSpy spy(box, SIGNAL(clicked(bool))); //... QListarguments = spy.takeFirst(); // take the first signal QVERIFY(arguments.at(0).toBool() == true); // verify the first argument
We can use installEventFilter to make mouse left button, right button, and middle button press invalid.
Example code:
class MousePressEater : public QObject { Q_OBJECT public: explicit MousePressEater(QObject *parent = nullptr); protected: bool eventFilter(QObject *obj, QEvent *event) override; }; bool MousePressEater::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::MouseButtonRelease) { QMouseEvent *mouseEvent = static_cast(event); qDebug("mouseEvent press %d", mouseEvent->button()); return true; } else { // standard event processing return QObject::eventFilter(obj, event); } } //... pressEater = new MousePressEater (this); ui->pushButton->installEventFilter( pressEater );